Skip to content

Commit f623a77

Browse files
committed
feat: update solutions to lc problem: No.0019. Remove Nth Node From End of List
1 parent 1ea24c1 commit f623a77

File tree

8 files changed

+243
-115
lines changed

8 files changed

+243
-115
lines changed

solution/0000-0099/0019.Remove Nth Node From End of List/README.md

+89-29
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666
class Solution:
6767
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
6868
dummy = ListNode(next=head)
69-
p = q = dummy
70-
for i in range(n):
71-
p = p.next
72-
while p.next:
73-
p, q = p.next, q.next
74-
q.next = q.next.next
69+
fast = slow = dummy
70+
for _ in range(n):
71+
fast = fast.next
72+
while fast.next:
73+
slow, fast = slow.next, fast.next
74+
slow.next = slow.next.next
7575
return dummy.next
7676
```
7777

@@ -93,15 +93,15 @@ class Solution:
9393
class Solution {
9494
public ListNode removeNthFromEnd(ListNode head, int n) {
9595
ListNode dummy = new ListNode(0, head);
96-
ListNode p = dummy, q = dummy;
96+
ListNode fast = dummy, slow = dummy;
9797
while (n-- > 0) {
98-
p = p.next;
98+
fast = fast.next;
9999
}
100-
while (p.next != null) {
101-
p = p.next;
102-
q = q.next;
100+
while (fast.next != null) {
101+
slow = slow.next;
102+
fast = fast.next;
103103
}
104-
q.next = q.next.next;
104+
slow.next = slow.next.next;
105105
return dummy.next;
106106
}
107107
}
@@ -124,16 +124,16 @@ class Solution {
124124
public:
125125
ListNode* removeNthFromEnd(ListNode* head, int n) {
126126
ListNode* dummy = new ListNode(0, head);
127-
ListNode* p = dummy;
128-
ListNode* q = dummy;
129-
while (n-- > 0) {
130-
p = p->next;
127+
ListNode* fast = dummy;
128+
ListNode* slow = dummy;
129+
while (n--) {
130+
fast = fast->next;
131131
}
132-
while (p->next != nullptr) {
133-
p = p->next;
134-
q = q->next;
132+
while (fast->next) {
133+
slow = slow->next;
134+
fast = fast->next;
135135
}
136-
q->next = q->next->next;
136+
slow->next = slow->next->next;
137137
return dummy->next;
138138
}
139139
};
@@ -150,22 +150,82 @@ public:
150150
* }
151151
*/
152152
func removeNthFromEnd(head *ListNode, n int) *ListNode {
153-
dummy := &ListNode{Val:0, Next:head}
154-
p := dummy
155-
q := dummy
153+
dummy := &ListNode{0, head}
154+
fast := dummy
155+
slow := dummy
156156
for n > 0 {
157-
p = p.Next
158-
n--
157+
fast = fast.Next
158+
n -= 1
159159
}
160-
for p.Next != nil {
161-
p = p.Next
162-
q = q.Next
160+
for fast.Next != nil {
161+
slow = slow.Next
162+
fast = fast.Next
163163
}
164-
q.Next = q.Next.Next
164+
slow.Next = slow.Next.Next
165165
return dummy.Next
166166
}
167167
```
168168

169+
### **JavaScript**
170+
171+
```js
172+
/**
173+
* Definition for singly-linked list.
174+
* function ListNode(val, next) {
175+
* this.val = (val===undefined ? 0 : val)
176+
* this.next = (next===undefined ? null : next)
177+
* }
178+
*/
179+
/**
180+
* @param {ListNode} head
181+
* @param {number} n
182+
* @return {ListNode}
183+
*/
184+
var removeNthFromEnd = function(head, n) {
185+
const dummy = new ListNode(0, head);
186+
let fast = dummy, slow = dummy;
187+
while (n--) {
188+
fast = fast.next;
189+
}
190+
while (fast.next) {
191+
slow = slow.next;
192+
fast = fast.next;
193+
}
194+
slow.next = slow.next.next;
195+
return dummy.next;
196+
};
197+
```
198+
199+
### **Ruby**
200+
201+
```rb
202+
# Definition for singly-linked list.
203+
# class ListNode
204+
# attr_accessor :val, :next
205+
# def initialize(val = 0, _next = nil)
206+
# @val = val
207+
# @next = _next
208+
# end
209+
# end
210+
# @param {ListNode} head
211+
# @param {Integer} n
212+
# @return {ListNode}
213+
def remove_nth_from_end(head, n)
214+
dummy = ListNode.new(0, head)
215+
fast = slow = dummy
216+
while n > 0
217+
fast = fast.next
218+
n -= 1
219+
end
220+
while fast.next
221+
slow = slow.next
222+
fast = fast.next
223+
end
224+
slow.next = slow.next.next
225+
return dummy.next
226+
end
227+
```
228+
169229
### **...**
170230

171231
```

solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md

+89-29
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
class Solution:
5757
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
5858
dummy = ListNode(next=head)
59-
p = q = dummy
60-
for i in range(n):
61-
p = p.next
62-
while p.next:
63-
p, q = p.next, q.next
64-
q.next = q.next.next
59+
fast = slow = dummy
60+
for _ in range(n):
61+
fast = fast.next
62+
while fast.next:
63+
slow, fast = slow.next, fast.next
64+
slow.next = slow.next.next
6565
return dummy.next
6666
```
6767

@@ -81,15 +81,15 @@ class Solution:
8181
class Solution {
8282
public ListNode removeNthFromEnd(ListNode head, int n) {
8383
ListNode dummy = new ListNode(0, head);
84-
ListNode p = dummy, q = dummy;
84+
ListNode fast = dummy, slow = dummy;
8585
while (n-- > 0) {
86-
p = p.next;
86+
fast = fast.next;
8787
}
88-
while (p.next != null) {
89-
p = p.next;
90-
q = q.next;
88+
while (fast.next != null) {
89+
slow = slow.next;
90+
fast = fast.next;
9191
}
92-
q.next = q.next.next;
92+
slow.next = slow.next.next;
9393
return dummy.next;
9494
}
9595
}
@@ -112,16 +112,16 @@ class Solution {
112112
public:
113113
ListNode* removeNthFromEnd(ListNode* head, int n) {
114114
ListNode* dummy = new ListNode(0, head);
115-
ListNode* p = dummy;
116-
ListNode* q = dummy;
117-
while (n-- > 0) {
118-
p = p->next;
115+
ListNode* fast = dummy;
116+
ListNode* slow = dummy;
117+
while (n--) {
118+
fast = fast->next;
119119
}
120-
while (p->next != nullptr) {
121-
p = p->next;
122-
q = q->next;
120+
while (fast->next) {
121+
slow = slow->next;
122+
fast = fast->next;
123123
}
124-
q->next = q->next->next;
124+
slow->next = slow->next->next;
125125
return dummy->next;
126126
}
127127
};
@@ -138,22 +138,82 @@ public:
138138
* }
139139
*/
140140
func removeNthFromEnd(head *ListNode, n int) *ListNode {
141-
dummy := &ListNode{Val:0, Next:head}
142-
p := dummy
143-
q := dummy
141+
dummy := &ListNode{0, head}
142+
fast := dummy
143+
slow := dummy
144144
for n > 0 {
145-
p = p.Next
146-
n--
145+
fast = fast.Next
146+
n -= 1
147147
}
148-
for p.Next != nil {
149-
p = p.Next
150-
q = q.Next
148+
for fast.Next != nil {
149+
slow = slow.Next
150+
fast = fast.Next
151151
}
152-
q.Next = q.Next.Next
152+
slow.Next = slow.Next.Next
153153
return dummy.Next
154154
}
155155
```
156156

157+
### **JavaScript**
158+
159+
```js
160+
/**
161+
* Definition for singly-linked list.
162+
* function ListNode(val, next) {
163+
* this.val = (val===undefined ? 0 : val)
164+
* this.next = (next===undefined ? null : next)
165+
* }
166+
*/
167+
/**
168+
* @param {ListNode} head
169+
* @param {number} n
170+
* @return {ListNode}
171+
*/
172+
var removeNthFromEnd = function(head, n) {
173+
const dummy = new ListNode(0, head);
174+
let fast = dummy, slow = dummy;
175+
while (n--) {
176+
fast = fast.next;
177+
}
178+
while (fast.next) {
179+
slow = slow.next;
180+
fast = fast.next;
181+
}
182+
slow.next = slow.next.next;
183+
return dummy.next;
184+
};
185+
```
186+
187+
### **Ruby**
188+
189+
```rb
190+
# Definition for singly-linked list.
191+
# class ListNode
192+
# attr_accessor :val, :next
193+
# def initialize(val = 0, _next = nil)
194+
# @val = val
195+
# @next = _next
196+
# end
197+
# end
198+
# @param {ListNode} head
199+
# @param {Integer} n
200+
# @return {ListNode}
201+
def remove_nth_from_end(head, n)
202+
dummy = ListNode.new(0, head)
203+
fast = slow = dummy
204+
while n > 0
205+
fast = fast.next
206+
n -= 1
207+
end
208+
while fast.next
209+
slow = slow.next
210+
fast = fast.next
211+
end
212+
slow.next = slow.next.next
213+
return dummy.next
214+
end
215+
```
216+
157217
### **...**
158218

159219
```

solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class Solution {
1212
public:
1313
ListNode* removeNthFromEnd(ListNode* head, int n) {
1414
ListNode* dummy = new ListNode(0, head);
15-
ListNode* p = dummy;
16-
ListNode* q = dummy;
17-
while (n-- > 0) {
18-
p = p->next;
15+
ListNode* fast = dummy;
16+
ListNode* slow = dummy;
17+
while (n--) {
18+
fast = fast->next;
1919
}
20-
while (p->next != nullptr) {
21-
p = p->next;
22-
q = q->next;
20+
while (fast->next) {
21+
slow = slow->next;
22+
fast = fast->next;
2323
}
24-
q->next = q->next->next;
24+
slow->next = slow->next->next;
2525
return dummy->next;
2626
}
2727
};

solution/0000-0099/0019.Remove Nth Node From End of List/Solution.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
* }
77
*/
88
func removeNthFromEnd(head *ListNode, n int) *ListNode {
9-
dummy := &ListNode{Val:0, Next:head}
10-
p := dummy
11-
q := dummy
9+
dummy := &ListNode{0, head}
10+
fast := dummy
11+
slow := dummy
1212
for n > 0 {
13-
p = p.Next
14-
n--
13+
fast = fast.Next
14+
n -= 1
1515
}
16-
for p.Next != nil {
17-
p = p.Next
18-
q = q.Next
16+
for fast.Next != nil {
17+
slow = slow.Next
18+
fast = fast.Next
1919
}
20-
q.Next = q.Next.Next
20+
slow.Next = slow.Next.Next
2121
return dummy.Next
2222
}

solution/0000-0099/0019.Remove Nth Node From End of List/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
class Solution {
1212
public ListNode removeNthFromEnd(ListNode head, int n) {
1313
ListNode dummy = new ListNode(0, head);
14-
ListNode p = dummy, q = dummy;
14+
ListNode fast = dummy, slow = dummy;
1515
while (n-- > 0) {
16-
p = p.next;
16+
fast = fast.next;
1717
}
18-
while (p.next != null) {
19-
p = p.next;
20-
q = q.next;
18+
while (fast.next != null) {
19+
slow = slow.next;
20+
fast = fast.next;
2121
}
22-
q.next = q.next.next;
22+
slow.next = slow.next.next;
2323
return dummy.next;
2424
}
2525
}

0 commit comments

Comments
 (0)