Skip to content

Commit 297ea52

Browse files
committed
🟢 Solve problem 83
1 parent 4b86b34 commit 297ea52

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎typescript/deleteDuplicates.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
5+
constructor(val?: number, next?: ListNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.next = next === undefined ? null : next;
8+
}
9+
}
10+
11+
const auxRemoveDuplicates = (node: ListNode | null): ListNode | null => {
12+
if (!node?.next) {
13+
return node;
14+
}
15+
16+
const valueToRemove: number = node.val;
17+
18+
while (node.next && node.next.val === valueToRemove) {
19+
node.next = node.next.next;
20+
}
21+
22+
return node;
23+
};
24+
25+
const deleteDuplicates = (head: ListNode | null): ListNode | null => {
26+
if (!head?.next) {
27+
return head;
28+
}
29+
30+
let current: ListNode | null = head;
31+
32+
while (current?.next) {
33+
if (current.val === current.next.val) {
34+
current = auxRemoveDuplicates(current);
35+
} else {
36+
current = current.next;
37+
}
38+
}
39+
40+
return head;
41+
};

0 commit comments

Comments
 (0)