File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments