Skip to content

Commit fd6d0d6

Browse files
committed
Remove Duplicates from Sorted List
1 parent ed98d74 commit fd6d0d6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Remove Duplicates from Sorted List.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*Given a sorted linked list, delete all duplicates such that each element appear only once.
2+
3+
For example,
4+
Given 1->1->2, return 1->2.
5+
Given 1->1->2->3->3, return 1->2->3.*/
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* function ListNode(val) {
10+
* this.val = val;
11+
* this.next = null;
12+
* }
13+
*/
14+
/**
15+
* @param {ListNode} head
16+
* @return {ListNode}
17+
*/
18+
var deleteDuplicates = function(head) {
19+
if(head){
20+
var ans = new ListNode(head.val)
21+
var curHead = head
22+
var curAns = ans
23+
while(curHead.next){
24+
if(curHead.val != curHead.next.val){
25+
curAns.next = new ListNode(curHead.next.val)
26+
curAns = curAns.next
27+
}
28+
curHead = curHead.next
29+
}
30+
return ans
31+
}
32+
return head
33+
};

0 commit comments

Comments
 (0)