Skip to content

Commit 2f30bab

Browse files
authored
feat: add solutions to lc problem: No.0019 (doocs#3901)
No.0019.Remove Nth Node From End of List
1 parent 66e1a30 commit 2f30bab

File tree

4 files changed

+153
-87
lines changed

4 files changed

+153
-87
lines changed

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

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,38 @@ var removeNthFromEnd = function (head, n) {
278278
};
279279
```
280280

281+
#### Swift
282+
283+
````swift
284+
/**
285+
* Definition for singly-linked list.
286+
* public class ListNode {
287+
* public var val: Int
288+
* public var next: ListNode?
289+
* public init() { self.val = 0; self.next = nil; }
290+
* public init(_ val: Int) { self.val = val; self.next = nil; }
291+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
292+
* }
293+
*/
294+
class Solution {
295+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
296+
let dummy = ListNode(0)
297+
dummy.next = head
298+
var fast: ListNode? = dummy
299+
var slow: ListNode? = dummy
300+
for _ in 0..<n {
301+
fast = fast?.next
302+
}
303+
while fast?.next != nil {
304+
fast = fast?.next
305+
slow = slow?.next
306+
}
307+
slow?.next = slow?.next?.next
308+
return dummy.next
309+
}
310+
}
311+
```
312+
281313
#### Ruby
282314

283315
```rb
@@ -311,43 +343,34 @@ end
311343
#### PHP
312344

313345
```php
314-
# Definition for singly-linked list.
315-
# class ListNode {
316-
# public $val;
317-
# public $next;
318-
319-
# public function __construct($val = 0, $next = null)
320-
# {
321-
# $this->val = $val;
322-
# $this->next = $next;
323-
# }
324-
# }
325-
346+
/**
347+
* Definition for a singly-linked list.
348+
* class ListNode {
349+
* public $val = 0;
350+
* public $next = null;
351+
* function __construct($val = 0, $next = null) {
352+
* $this->val = $val;
353+
* $this->next = $next;
354+
* }
355+
* }
356+
*/
326357
class Solution {
327358
/**
328359
* @param ListNode $head
329-
* @param int $n
360+
* @param Integer $n
330361
* @return ListNode
331362
*/
332-
333363
function removeNthFromEnd($head, $n) {
334-
$dummy = new ListNode(0);
335-
$dummy->next = $head;
336-
337-
$first = $dummy;
338-
$second = $dummy;
339-
340-
for ($i = 0; $i <= $n; $i++) {
341-
$second = $second->next;
364+
$dummy = new ListNode(0, $head);
365+
$fast = $slow = $dummy;
366+
for ($i = 0; $i < $n; $i++) {
367+
$fast = $fast->next;
342368
}
343-
344-
while ($second != null) {
345-
$first = $first->next;
346-
$second = $second->next;
369+
while ($fast->next !== null) {
370+
$fast = $fast->next;
371+
$slow = $slow->next;
347372
}
348-
349-
$first->next = $first->next->next;
350-
373+
$slow->next = $slow->next->next;
351374
return $dummy->next;
352375
}
353376
}
@@ -358,3 +381,4 @@ class Solution {
358381
<!-- solution:end -->
359382

360383
<!-- problem:end -->
384+
````

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

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,38 @@ var removeNthFromEnd = function (head, n) {
275275
};
276276
```
277277

278+
#### Swift
279+
280+
````swift
281+
/**
282+
* Definition for singly-linked list.
283+
* public class ListNode {
284+
* public var val: Int
285+
* public var next: ListNode?
286+
* public init() { self.val = 0; self.next = nil; }
287+
* public init(_ val: Int) { self.val = val; self.next = nil; }
288+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
289+
* }
290+
*/
291+
class Solution {
292+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
293+
let dummy = ListNode(0)
294+
dummy.next = head
295+
var fast: ListNode? = dummy
296+
var slow: ListNode? = dummy
297+
for _ in 0..<n {
298+
fast = fast?.next
299+
}
300+
while fast?.next != nil {
301+
fast = fast?.next
302+
slow = slow?.next
303+
}
304+
slow?.next = slow?.next?.next
305+
return dummy.next
306+
}
307+
}
308+
```
309+
278310
#### Ruby
279311

280312
```rb
@@ -308,43 +340,34 @@ end
308340
#### PHP
309341

310342
```php
311-
# Definition for singly-linked list.
312-
# class ListNode {
313-
# public $val;
314-
# public $next;
315-
316-
# public function __construct($val = 0, $next = null)
317-
# {
318-
# $this->val = $val;
319-
# $this->next = $next;
320-
# }
321-
# }
322-
343+
/**
344+
* Definition for a singly-linked list.
345+
* class ListNode {
346+
* public $val = 0;
347+
* public $next = null;
348+
* function __construct($val = 0, $next = null) {
349+
* $this->val = $val;
350+
* $this->next = $next;
351+
* }
352+
* }
353+
*/
323354
class Solution {
324355
/**
325356
* @param ListNode $head
326-
* @param int $n
357+
* @param Integer $n
327358
* @return ListNode
328359
*/
329-
330360
function removeNthFromEnd($head, $n) {
331-
$dummy = new ListNode(0);
332-
$dummy->next = $head;
333-
334-
$first = $dummy;
335-
$second = $dummy;
336-
337-
for ($i = 0; $i <= $n; $i++) {
338-
$second = $second->next;
361+
$dummy = new ListNode(0, $head);
362+
$fast = $slow = $dummy;
363+
for ($i = 0; $i < $n; $i++) {
364+
$fast = $fast->next;
339365
}
340-
341-
while ($second != null) {
342-
$first = $first->next;
343-
$second = $second->next;
366+
while ($fast->next !== null) {
367+
$fast = $fast->next;
368+
$slow = $slow->next;
344369
}
345-
346-
$first->next = $first->next->next;
347-
370+
$slow->next = $slow->next->next;
348371
return $dummy->next;
349372
}
350373
}
@@ -355,3 +378,4 @@ class Solution {
355378
<!-- solution:end -->
356379

357380
<!-- problem:end -->
381+
````
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
# Definition for singly-linked list.
2-
# class ListNode {
3-
# public $val;
4-
# public $next;
5-
6-
# public function __construct($val = 0, $next = null)
7-
# {
8-
# $this->val = $val;
9-
# $this->next = $next;
10-
# }
11-
# }
12-
1+
/**
2+
* Definition for a singly-linked list.
3+
* class ListNode {
4+
* public $val = 0;
5+
* public $next = null;
6+
* function __construct($val = 0, $next = null) {
7+
* $this->val = $val;
8+
* $this->next = $next;
9+
* }
10+
* }
11+
*/
1312
class Solution {
1413
/**
1514
* @param ListNode $head
16-
* @param int $n
15+
* @param Integer $n
1716
* @return ListNode
1817
*/
19-
2018
function removeNthFromEnd($head, $n) {
21-
$dummy = new ListNode(0);
22-
$dummy->next = $head;
23-
24-
$first = $dummy;
25-
$second = $dummy;
26-
27-
for ($i = 0; $i <= $n; $i++) {
28-
$second = $second->next;
19+
$dummy = new ListNode(0, $head);
20+
$fast = $slow = $dummy;
21+
for ($i = 0; $i < $n; $i++) {
22+
$fast = $fast->next;
2923
}
30-
31-
while ($second != null) {
32-
$first = $first->next;
33-
$second = $second->next;
24+
while ($fast->next !== null) {
25+
$fast = $fast->next;
26+
$slow = $slow->next;
3427
}
35-
36-
$first->next = $first->next->next;
37-
28+
$slow->next = $slow->next->next;
3829
return $dummy->next;
3930
}
4031
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
13+
let dummy = ListNode(0)
14+
dummy.next = head
15+
var fast: ListNode? = dummy
16+
var slow: ListNode? = dummy
17+
for _ in 0..<n {
18+
fast = fast?.next
19+
}
20+
while fast?.next != nil {
21+
fast = fast?.next
22+
slow = slow?.next
23+
}
24+
slow?.next = slow?.next?.next
25+
return dummy.next
26+
}
27+
}

0 commit comments

Comments
 (0)