-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20-链表去重.js
38 lines (34 loc) · 882 Bytes
/
20-链表去重.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
function ListNode(val) {
this.val = val
this.next = null
}
function createLinkList(nums) {
let head = new ListNode(nums[0], null)
let pointer = head
for(let i = 1; i < nums.length; i ++) {
pointer.next = new ListNode(nums[i])
pointer = pointer.next
}
return head
}
const listNode = createLinkList([1, 1, 2, 3, 3])
function deduplicateLinkList(listNode) {
let pointer = new ListNode("666")
const head = pointer
const set = new Set()
let next = listNode
while(next !== null) {
if(set.has(next.val)) {
next = next.next
pointer.next = next
} else {
set.add(next.val)
pointer.next = next
pointer = pointer.next
next = next.next
}
}
return head.next
}
const result = deduplicateLinkList(listNode)
console.log(result)