-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ70.js
57 lines (44 loc) · 1.07 KB
/
Q70.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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.
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
function arrToList(arr) {
// console.log(arr.length);
if (arr.length === 0) {
return null
}
let head = new ListNode();
head.val = arr[0];
let top = head;
// console.log(top);
for (let i = 1; i < arr.length; i++) {
head.next = new ListNode(arr[i]);
head = head.next;
}
return top;
}
var removeElements = function (head, val) {
let arr = [];
let req;
while (head) {
arr.push(head.val);
head = head.next;
}
req = arr.filter((i) => {
if (i !== val) {
return i;
}
});
return arrToList(req);
};
let head = arrToList([7,7,7,7]);
let val = 7;
let answer = removeElements(head, val);
console.log(answer);
// while (answer) {
// console.log(answer.val);
// answer = answer.next;
// }
// console.log(arrToList([]));