移除链表中的元素

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

Example:

1
2
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const removeElements = function(head, val) {
if (!head){
return head;
}
while (head && head.val == val){
head = head.next;
}
let current = head;
while (current){
if (current.next && current.next.val == val){
current.next = current.next.next;
}
else{
current = current.next;
}
}
return head;
};