-
Notifications
You must be signed in to change notification settings - Fork 0
/
203.ts
44 lines (41 loc) · 1.11 KB
/
203.ts
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
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}
function removeElements(head: ListNode | null, val: number): ListNode | null {
const dummy = new ListNode(0, head)
let curr = head
let prev = dummy
while (curr) {
if (curr.val == val) {
prev.next = curr.next
curr = prev.next
} else {
prev = curr
curr = curr.next
}
}
return dummy.next
};
printList(removeElements(makeList([1, 1, 1, 1, 1, 1, 6]), 1))
function printList(mergeList: ListNode) {
while (mergeList) {
console.log(mergeList.val)
mergeList = mergeList.next
}
console.log(" ")
}
function makeList(first: number[]) {
if (first.length == 0) return null
let list = new ListNode(first[first.length - 1])
for (let i = first.length - 2; i >= 0; i--) {
const temp = new ListNode(first[i])
temp.next = list
list = temp
}
return list
}