Remove all elements from a linked list of integers that have valueval.

Example
Given:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6
Return:1 --> 2 --> 3 --> 4 --> 5

Credits:
Special thanks to@mithmattfor adding this problem and creating all test cases.

Subscribeto see which companies asked this question.

Hide Tags

Linked List

Hide Similar Problems

(E) Remove Element

(E) Delete Node in a Linked List


 public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        ListNode pointer = head;
        while (pointer.next != null) {
               if (pointer.next.val == val) pointer.next = pointer.next.next;
               else pointer = pointer.next;
           }
        return head.val == val ? head.next : head;
 }

results matching ""

    No results matching ""