-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-nth-node-from-end-of-list.js
175 lines (161 loc) · 4.1 KB
/
remove-nth-node-from-end-of-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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Source: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
* Tags: [Linked List,Two Pointers]
* Level: Easy
* Title: Remove Nth Node From End of List
* Auther: @imcoddy
* Content: Given a linked list, remove the nth node from the end of list and return its head.
*
*
* For example,
*
*
* Given linked list: 1->2->3->4->5, and n = 2.
*
* After removing the second node from the end, the linked list becomes 1->2->3->5.
*
*
*
* Note:
* Given n will always be valid.
* Try to do this in one pass.
*/
var util = require('./util.js');
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
/**
* Memo: Set two pointers as fast and slow, keep slow is n nodes slower than fast, and keep moving two pointers till fast reaches the end.
* Runtime: 150ms
* Tests: 207 test cases passed
* Rank: S
*/
var removeNthFromEnd = function(head, n) {
var dummy = new ListNode(null);
dummy.next = head;
var fast = dummy;
var slow = dummy;
while (fast.next) {
fast = fast.next;
if (n-- <= 0) {
slow = slow.next;
}
}
slow.next = slow.next.next;
return dummy.next;
};
/**
* Memo: Find out length of the linked list first and search the length - n th element and delete it.
* Complex: O(n)
* Runtime: 152ms
* Tests: 207 test cases passed
* Rank: A
* Updated: 2015-06-16
*/
var removeNthFromEnd = function(head, n) {
var count = 0;
var p = head;
while (p) {
p = p.next;
count++;
}
count = count - n;
if (count === 0) { // remove the first element
return head.next;
}
if (count > 0) {
p = head;
while (count > 1) {
p = p.next;
count--;
}
p.next = p.next.next;
}
//count < 0 is invalid, since the n will always be valid so ignore this case
return head;
};
/**
* Memo: Same solution as above using dummy head.
* Complex: O(n)
* Runtime: 180ms
* Tests: 207 test cases passed
* Rank: B
* Updated: 2015-06-16
*/
var removeNthFromEnd = function(head, n) {
var p = head;
var count = 0;
while (p) {
p = p.next;
count++;
}
var dummy = new ListNode(null);
dummy.next = head;
count = count - n;
p = dummy;
while (count > 0) {
p = p.next;
count--;
}
p.next = p.next.next;
return dummy.next;
};
/**
* Memo: Use fast and slow pointers and keep n nodes between them. When fast reaches the end, slow will be the element to remove its next node (if exists).
* Complex: O(n)
* Runtime: 148ms
* Tests: 207 test cases passed
* Rank: S
* Updated: 2015-06-16
*/
var removeNthFromEnd = function(head, n) {
var dummy = new ListNode(null);
dummy.next = head;
var fast = dummy;
var slow = dummy;
while (fast) {
fast = fast.next;
if (n-- < 0) slow = slow.next;
}
if (slow.next) slow.next = slow.next.next;
return dummy.next;
};
/**
* Memo: Use fast and slow pointers and keep n nodes between them. When fast reaches the end, slow will be the element to remove its next node (if exists).
* Complex: O(n)
* Runtime: 148ms
* Tests: 207 test cases passed
* Rank: S
* Updated: 2015-08-20
*/
var removeNthFromEnd = function(head, n) {
var dummy = new ListNode(null);
dummy.next = head;
var fast = dummy;
var slow = dummy;
while (fast = fast.next)
if (--n < 0) slow = slow.next;
if (slow.next) slow.next = slow.next.next;
return dummy.next;
};
function ListNode(val) {
this.val = val;
this.next = null;
}
var should = require('should');
console.time('Runtime');
util.lta(removeNthFromEnd(util.atl([1]), 1)).should.eql([]);
util.lta(removeNthFromEnd(util.atl([1, 2]), 1)).should.eql([1]);
util.lta(removeNthFromEnd(util.atl([1, 2]), 2)).should.eql([2]);
util.lta(removeNthFromEnd(util.atl([1, 2, 3, 4, 5]), 2)).should.eql([1, 2, 3, 5]);
util.lta(removeNthFromEnd(util.atl([1, 2, 3, 4, 5]), 1)).should.eql([1, 2, 3, 4]);
console.timeEnd('Runtime');