Skip to content

Commit 2a1105b

Browse files
committed
✅ [83] Runtime 64 ms Beats 80.79% Memory 44.9 MB Beats 8.25%
1 parent 54daef1 commit 2a1105b

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

83/my_solution.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class ListNode {
2+
constructor(val, next) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.next = (next === undefined ? null : next);
5+
}
6+
}
7+
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
const deleteDuplicates = (head) => {
13+
let map = new Map(), dummy = new ListNode(), result = dummy;
14+
15+
// go thru all the nodes, check if they are in the map. if yes: skip, if no, then add.
16+
while (head) {
17+
console.log(map)
18+
console.log(map.get(head.val))
19+
if (!map.has(head.val)) {
20+
console.log(`${head.val} cant be found in map`)
21+
// let tmp = head.next
22+
// head.next = null;
23+
map.set(head.val, 0);
24+
dummy.next = new ListNode(head.val);
25+
// head = tmp; // move head up
26+
dummy = dummy.next; //
27+
} else {
28+
console.log(`Found ${head.val}!`)
29+
// in the map, stop the link
30+
}
31+
32+
head = head.next; // move head up
33+
}
34+
35+
console.log(map)
36+
37+
console.log("result")
38+
console.log(result)
39+
40+
return result.next;
41+
42+
}
43+
44+
deleteDuplicates(
45+
new ListNode(1,
46+
new ListNode(1,
47+
new ListNode(2,
48+
new ListNode(3
49+
)
50+
)
51+
)
52+
)
53+
)

83/solution.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ListNode {
2+
constructor(val, next) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.next = (next === undefined ? null : next);
5+
}
6+
}
7+
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
const deleteDuplicates = (head) => {
13+
let map = new Map(), dummy = new ListNode(), result = dummy;
14+
15+
while (head) {
16+
if (!map.has(head.val)) {
17+
map.set(head.val, 0);
18+
dummy.next = new ListNode(head.val);
19+
dummy = dummy.next;
20+
}
21+
22+
head = head.next;
23+
}
24+
25+
return result.next;
26+
}

0 commit comments

Comments
 (0)