-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path203.js
More file actions
29 lines (28 loc) · 712 Bytes
/
Copy path203.js
File metadata and controls
29 lines (28 loc) · 712 Bytes
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
26
27
28
29
/**
* 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) {
if(head == null) return null;
let current = new ListNode(-1);
// to ensure that there is always next node, we create a dummy node in front of head
current.next = head;
head = current;
while(current.next){
if(current.next.val == val){
current.next = current.next.next;
}
else {
current = current.next;
}
}
return head.next //exclude dummy(current)
};