-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path82.js
More file actions
33 lines (32 loc) · 702 Bytes
/
Copy path82.js
File metadata and controls
33 lines (32 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
let newHead = new ListNode();
let cur = newHead;
let duplicate = 0;
while(head){
if(head.next && head.val == head.next.val){
duplicate++;
}else {
if(duplicate == 0){
cur.next = head;
cur = cur.next;
}
duplicate = 0;
}
head = head.next;
}
cur.next = null;
return newHead.next;
};
//time complexity O(n)
//space O(1)