Leetcode 203 - Remove linked list elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
img

1
2
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]

Note: We don’t really need double pointers to solve this because the memory of linked lists is not really consecutive. Just using one pointer to iterate is enough.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var removeElements = function(head, val) {
let dummy = new ListNode(0, head);
let pointer = dummy;
if (!head) return null;
while (pointer.next) {
if (pointer.next.val === val) {
pointer.next = pointer.next.next;
continue;
}
pointer = pointer.next;
}
return dummy.next;
};