Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 163f2e6

Browse files
authored
Create 203-remove-linked-list-elements.js
Solved remove linked linked list in JS.
1 parent 3cfb9a5 commit 163f2e6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// problem link https://leetcode.com/problems/remove-linked-list-elements/
2+
// time complexity O(n).
3+
4+
var removeElements = function(head, val) {
5+
// let currunt = head;
6+
let pre = new ListNode(null, head);
7+
let dummy = pre;
8+
while(head) {
9+
if(head.val == val) {
10+
while(head && head.val == val) {
11+
head = head.next;
12+
}
13+
pre.next = head;
14+
pre = head;
15+
if(head) {
16+
temp = head;
17+
head = head.next;
18+
temp = null;
19+
}
20+
continue;
21+
}
22+
pre = pre.next;
23+
head = head.next;
24+
}
25+
26+
return dummy.next;
27+
};

0 commit comments

Comments
 (0)