1
/**
2
* Definition for singly-linked list.
3
* function ListNode(val, next) {
4
* this.val = (val===undefined ? 0 : val)
5
* this.next = (next===undefined ? null : next)
6
* }
7
*/
8
/**
9
* @param {ListNode} head
10
* @param {number} val
11
* @return {ListNode}
12
*/
13
var removeElements = function (head, val) {
14
if (!head) return null;
15

16
//if the val is on the beginning delete it
17
while (head && head.val === val) head = head.next;
18

19
let current = head;
20
let next = head.next;
21
//travers the liste and delete any node has this val
22
while (next) {
23
if (next.val === val) {
24
current.next = next.next;
25
} else current = next;
26

27
next = next.next;
28
}
29

30
return head;
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0