-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-linked-list-elements.js
119 lines (109 loc) · 2.87 KB
/
remove-linked-list-elements.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
/**
* Source: https://leetcode.com/problems/remove-linked-list-elements/
* Tags: [Linked List]
* Level: Easy
* Title: Remove Linked List Elements
* Auther: @imcoddy
* Content: Remove all elements from a linked list of integers that have value val.
*
* Example
* Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
* Return: 1 --> 2 --> 3 --> 4 --> 5
*
*
* Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
/**
* Explanation: Many traps in here.
* 1. Head could be needed to delete
* 2. Dont move the pointer until next doesn't need to delete
* Runtime: 177ms
* Rank: S
*/
var removeElements = function(head, val) {
// remove when head is element of val
while (head && head.val === val) {
head = head.next;
}
var p = head;
while (p) {
while (p.next && p.next.val === val) {
var d = p.next;
p.next = d.next;
d = null;
}
p = p.next;
}
return head;
};
/**
* Memo: Use prev and p to search if p.val is equal to val, if so, delete p and keep searching till the end.
* Complex: O(n)
* Runtime: 204ms
* Tests: 63 test cases passed
* Rank: B
* Updated: 2015-06-16
*/
var removeElements = function(head, val) {
var dummy = new ListNode(null);
dummy.next = head;
var prev = dummy;
var p = dummy.next;
while (p) {
if (p.val === val) {
prev.next = p.next;
} else {
prev = p;
}
p = p.next;
}
return dummy.next;
};
/**
* Memo: Improve from above solution by keep moving if p.next equal val, works better if continous elements need to be removed.
* Complex: O(n)
* Runtime: 168ms
* Tests: 63 test cases passed
* Rank: S
* Updated: 2015-06-16
*/
var removeElements = function(head, val) {
var dummy = new ListNode(null);
dummy.next = head;
var prev = dummy;
var p = dummy.next;
while (p) {
while (p && p.val === val) p = p.next;
prev.next = p;
if (p) {
prev = prev.next;
p = p.next;
}
}
return dummy.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
var util = require("./util.js");
var should = require('should');
console.time('Runtime');
util.lta(removeElements(util.atl([1, 2, 3, 4, 5]), 2)).should.eql([1, 3, 4, 5]);
util.lta(removeElements(util.atl([1, 2, 3, 2, 4, 5, 2, 2]), 2)).should.eql([1, 3, 4, 5]);
util.lta(removeElements(util.atl([1, 1, 1, 1]), 1)).should.eql([]);
util.lta(removeElements(util.atl([1, 2, 1, 2]), 2)).should.eql([1, 1]);
util.lta(removeElements(util.atl([1, 2, 3, 4, 5]), 2)).should.eql([1, 3, 4, 5]);
console.timeEnd('Runtime');