Skip to content

Commit 3ea2cdc

Browse files
committed
19. Remove Nth Node From End of List
1 parent b006fff commit 3ea2cdc

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [19. Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/)
2+
###### tags: `Medium` `Leetcode` `two pointers`
3+
4+
## 題目
5+
Given the head of a linked list, remove the nth node from the end of the list and return its head.
6+
7+
Example 1:
8+
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
9+
```
10+
Input: head = [1,2,3,4,5], n = 2
11+
Output: [1,2,3,5]
12+
```
13+
14+
Example 2:
15+
```
16+
Input: head = [1], n = 1
17+
Output: []
18+
```
19+
20+
Example 3:
21+
```
22+
Input: head = [1,2], n = 1
23+
Output: [1]
24+
```
25+
26+
Constraints:
27+
* The number of nodes in the list is sz.
28+
* 1 <= sz <= 30
29+
* 0 <= Node.val <= 100
30+
* 1 <= n <= sz
31+
32+
33+
## 題目大意
34+
找尋單linked list的 倒數第 n 個元素並刪除.
35+
返回該 linked list的頭節點
36+
37+
## 解題思路
38+
先讓 fast走 `k` 步, 然後 `fast slow 同速前進`
39+
這樣當fast走到nil時, slow所在位置就是在倒數第 k 的節點
40+
41+
## 來源
42+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/
43+
44+
## 解答
45+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go
46+
47+
```go
48+
package removenthnodefromendoflist
49+
50+
/**
51+
* Definition for singly-linked list.
52+
* type ListNode struct {
53+
* Val int
54+
* Next *ListNode
55+
* }
56+
*/
57+
58+
type ListNode struct {
59+
Val int
60+
Next *ListNode
61+
}
62+
63+
func RemoveNthFromEnd(head *ListNode, n int) *ListNode {
64+
dummyHead := &ListNode{Next: head}
65+
preSlow, slow, fast := dummyHead, head, head
66+
for fast != nil {
67+
if n <= 0 {
68+
// 先讓 fast走 `k` 步, 然後 `fast slow 同速前進`
69+
// 這樣當fast走到nil時, slow所在位置就是在倒數第 k 的節點
70+
preSlow = slow
71+
slow = slow.Next
72+
}
73+
n--
74+
fast = fast.Next
75+
}
76+
preSlow.Next = slow.Next
77+
return dummyHead.Next
78+
}
79+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package removenthnodefromendoflist
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* type ListNode struct {
6+
* Val int
7+
* Next *ListNode
8+
* }
9+
*/
10+
11+
type ListNode struct {
12+
Val int
13+
Next *ListNode
14+
}
15+
16+
func RemoveNthFromEnd(head *ListNode, n int) *ListNode {
17+
dummyHead := &ListNode{Next: head}
18+
preSlow, slow, fast := dummyHead, head, head
19+
for fast != nil {
20+
if n <= 0 {
21+
// 先讓 fast走 `k` 步, 然後 `fast slow 同速前進`
22+
// 這樣當fast走到nil時, slow所在位置就是在倒數第 k 的節點
23+
preSlow = slow
24+
slow = slow.Next
25+
}
26+
n--
27+
fast = fast.Next
28+
}
29+
preSlow.Next = slow.Next
30+
return dummyHead.Next
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package removenthnodefromendoflist
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestRemoveNthFromEnd(t *testing.T) {
9+
a := &ListNode{1, nil}
10+
b := &ListNode{2, nil}
11+
c := &ListNode{3, nil}
12+
d := &ListNode{4, nil}
13+
e := &ListNode{5, nil}
14+
15+
a.Next = b
16+
b.Next = c
17+
c.Next = d
18+
d.Next = e
19+
e.Next = nil
20+
21+
tests := []struct {
22+
arg1 *ListNode
23+
arg2 int
24+
want []int
25+
}{
26+
{
27+
arg1: a,
28+
arg2: 2,
29+
want: []int{1, 2, 3, 5},
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
got := RemoveNthFromEnd(tt.arg1, tt.arg2)
35+
36+
gotArr := []int{}
37+
for got != nil {
38+
gotArr = append(gotArr, got.Val)
39+
got = got.Next
40+
}
41+
if !reflect.DeepEqual(gotArr, tt.want) {
42+
t.Errorf("got = %v, want = %v", gotArr, tt.want)
43+
}
44+
}
45+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
| No. | Title | Solution | Difficulty | Time | Space | Topic |
4949
|-----|:-----:|:--------:|------------|------|-------|-------|
50+
| 0019 | [Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0019.Remove-Nth-Node-From-End-of-List) | Medium | O(n) | O(1) | Linked List, Two Pointers |
5051
| 0141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0141.Linked-List-Cycle) | Easy | O(n) | O(1) | Linked List, Two Pointers |
5152
| 0142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
5253
| 0876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0876.Middle-of-the-Linked-List) | Easy | | | Linked List, Two Pointers |
@@ -137,6 +138,7 @@ https://labuladong.gitee.io/algo/2/20/56/
137138

138139
| No. | Title | Solution | Difficulty | Time | Space | Topic |
139140
|-----|:-----:|:--------:|------------|------|-------|-------|
141+
| 0019 | [Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0019.Remove-Nth-Node-From-End-of-List) | Medium | O(n) | O(1) | Linked List, Two Pointers |
140142
| 0141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0141.Linked-List-Cycle) | Easy | O(n) | O(1) | Linked List, Two Pointers |
141143
| 0142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
142144
| 0344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0344.Reverse-String) | Easy | O(n) | O(1) | Two Pointers |

0 commit comments

Comments
 (0)