-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-duplicates-from-sorted-list-ii.js
105 lines (91 loc) · 2.69 KB
/
remove-duplicates-from-sorted-list-ii.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Source: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
* Tags: [Linked List]
* Level: Medium
* Title: Remove Duplicates from Sorted List II
* Auther: @imcoddy
* Content: 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.
*/
var util = require("./util.js");
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
/**
* Memo: find the non-duplicate node and append it to tail, note that need to set tail to null as the rest might need to be deleted
* Runtime: 151ms
* Tests: 166 test cases passed
* Rank: S
*/
var deleteDuplicates = function(head) {
if (!head) return null;
var dummy = new ListNode(null);
var tail = dummy;
var p = head;
var duplicate = false;
while (p) {
while (p.next && p.val === p.next.val) {
p = p.next;
duplicate = true;
}
if (!duplicate) {
tail.next = p;
tail = tail.next;
}
p = p.next;
duplicate = false;
}
tail.next = null;
return dummy.next;
};
/**
* Memo: Check if the element is duplicated or not. If not, append it to the list.
* Complex: O(n)
* Runtime: 164ms
* Tests: 166 test cases passed
* Rank: A
* Updated: 2015-06-20
*/
var deleteDuplicates = function(head) {
if (!head || !head.next) return head;
var dummy = new ListNode(null);
var tail = dummy;
var p = head;
while (p) {
var start = p;
while (p && p.next && p.next.val === p.val) {
p = p.next;
}
var next = p.next;
if (start === p) {
tail.next = p;
tail = tail.next;
tail.next = null;
}
p = next;
}
return dummy.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([1, 1, 2, 2, 2]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([1, 2, 2, 2]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([1, 1, 1, 3, 3, 5]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([1, 1, 1, 3, 5]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([0, 1, 1, 1, 3, 5]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([]))));
console.log(util.linkListToString(deleteDuplicates(util.arrayToLinkList([1]))));