-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathremove-duplicates-from-sorted-list-ii.js
More file actions
44 lines (42 loc) · 1.07 KB
/
Copy pathremove-duplicates-from-sorted-list-ii.js
File metadata and controls
44 lines (42 loc) · 1.07 KB
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
34
35
36
37
38
39
40
41
42
43
44
// Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
//
//
// For example,
// Given 1->2->3->3->4->4->5, return 1->2->5.
// Given 1->1->1->2->3, return 2->3.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
var fakeHead = new ListNode(-1);
fakeHead.next = head;
head = fakeHead;
var isDup = false;
while (head.next) {
var current = head.next;
while (current.next) {
if (current.val === current.next.val) {
current = current.next;
isDup = true;
} else {
break;
}
}
if (isDup) {
head.next = current.next;
} else {
head.next = current;
head = head.next;
}
isDup = false;
}
return fakeHead.next;
};