-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicates.js
More file actions
40 lines (31 loc) · 995 Bytes
/
duplicates.js
File metadata and controls
40 lines (31 loc) · 995 Bytes
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
/*
Given the head of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.
Return the linked list after the deletions.
Example 1:
Input: head = [1,2,3,2]
Output: [1,3]
Explanation: 2 appears twice in the linked list, so all 2's should be deleted. After deleting all 2's, we are left with [1,3].
*/
function deleteDuplicates(head) {
let clone = head
let map = {}
while (clone !== null) {
if (!map[clone.val]) {
map[clone.val] = 1
} else {
map[clone.val]++
}
clone = clone.next
}
let prev = new ListNode(-1, head)
clone = head
while (clone !== null) {
//check if have next and if the next value is in the map
while (clone.next && map[clone.next.val] > 1) {
clone.next = clone.next.next
}
clone = clone.next
}
return prev.next
}
console.log(deleteDuplicates([1, 2, 3, 2])) // [1,3]