-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-duplicates-from-sorted-list.js
132 lines (120 loc) · 2.88 KB
/
remove-duplicates-from-sorted-list.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Source: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
* Tags: [Linked List]
* Level: Easy
* Updated: 2015-04-24
* Title: Remove Duplicates from Sorted List
* Auther: @imcoddy
* Content: Given a sorted linked list, delete all duplicates such that each element appear only once.
*
*
* For example,
* Given 1->1->2, return 1->2.
* Given 1->1->2->3->3, return 1->2->3.
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
var util = require("./util.js");
/**
* @param {ListNode} head
* @return {ListNode}
*/
/**
* Memo:
* Complex: O(n)
* Runtime: 158ms
* Tests: 164 test cases passed
* Rank: S
*/
var deleteDuplicates = function(head) {
var node = head;
while (node) {
var next = node.next;
while (next && node.val === next.val) {
node.next = next.next;
next = null;
next = node.next;
}
node = node.next;
}
return head;
};
/**
* Memo: Use dummy head to solve
* Complex: O(n)
* Runtime: 147ms
* Tests: 164 test cases passed
* Rank: S
*/
var deleteDuplicatesDummy = function(head) {
var dummy = new ListNode(null);
var tail = dummy;
tail.next = head;
while (tail) {
while (tail.next && tail.next.val === tail.val) {
tail.next = tail.next.next; // remove next node if val is the same
}
tail = tail.next; // move tail to last node
}
return dummy.next;
};
/**
* Memo: Remove next element if it is the same as current one.
* Complex: O(n)
* Runtime: 168ms
* Tests: 164 test cases passed
* Rank: A
* Updated: 2015-06-16
*/
var deleteDuplicates = function(head) {
var tail = head;
while (tail) {
while (tail.next && tail.next.val === tail.val) {
tail.next = tail.next.next;
}
tail = tail.next;
}
return head;
};
/**
* Memo: Keep moving next node until it reaches either null or different value.
* Complex: O(n)
* Runtime: 168ms
* Tests: 164 test cases passed
* Rank: S
* Updated: 2015-06-17
*/
var deleteDuplicates = function(head) {
var node = head;
while (node) {
var next = node.next;
while (next && next.val === node.val) {
next = next.next;
}
node.next = next;
node = node.next;
}
return head;
};
var deleteDuplicates = function(head) {
var tail = head;
while (tail) {
while (tail.next && tail.next.val === tail.val) tail.next = tail.next.next;
tail = tail.next;
}
return head;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
var should = require('should');
console.time('Runtime');
util.lta(deleteDuplicates(util.atl([1, 2, 3, 4]))).should.eql([1, 2, 3, 4]);
util.lta(deleteDuplicates(util.atl([1, 1, 2, 2, 3, 3, 4]))).should.eql([1, 2, 3, 4]);
console.timeEnd('Runtime');