Linked List practice

LinkedList Delete first element

Given a linked list. Delete the first appearance of the given integer.

Definition of a single linked list:

// Java
class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
// C++
class ListNode
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}
// C
struct ListNode
  int val;
  struct ListNode *next;

Implement a function DeleteFirstElement that deletes the first appearance of element. If element is not in a list, do nothing. Function DeleteFirstElement must return the pointer to the head of updated list.

// Java
ListNode DeleteFirstElement(ListNode head, int element)
// C, C++
ListNode* DeleteFirstElement(ListNode* head, int element)



Let's delete element = 2. Function DeleteFirstElement returns the pointer to the head of updated list:


Time limit 1 second
Memory limit 128 MiB
Author Michael Medvediev