Skip to content

Commit 56ecf93

Browse files
committed
added removeDuplicatesFromSortedLinkedList easy leetcode solution
1 parent 379cd59 commit 56ecf93

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const LinkedList = require('./linkedList');
2+
3+
// Given a sorted linked list, delete all duplicates such that each element appear only once.
4+
// Example 1:
5+
// Input: 1->1->2
6+
// Output: 1->2
7+
// Example 2:
8+
// Input: 1->1->2->3->3
9+
// Output: 1->2->3
10+
11+
const linkedList = new LinkedList(1).addValues([1,2]);
12+
const linkedList2 = new LinkedList(1).addValues([1,2,3,3]);
13+
14+
const removeDuplicates = (head) => {
15+
if (!head) return head;
16+
let current = head;
17+
while (current.next) {
18+
if (current.value === current.next.value) {
19+
current.next = current.next.next;
20+
} else {
21+
current = current.next;
22+
}
23+
}
24+
return head;
25+
}
26+
27+
console.log(removeDuplicates(linkedList));
28+
console.log(removeDuplicates(linkedList2));

0 commit comments

Comments
 (0)