Skip to content

Commit 86fac4e

Browse files
committed
Update 0019、0066
1 parent 7f0a3b5 commit 86fac4e

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,44 @@
22

33
## 题目
44

5-
Given a linked list, remove the n-th node from the end of list and return its head.
5+
Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
66

7-
Example:
7+
**Follow up:** Could you do this in one pass?
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
12+
13+
```
14+
Input: head = [1,2,3,4,5], n = 2
15+
Output: [1,2,3,5]
816
917
```
10-
Given linked list: 1->2->3->4->5, and n = 2.
1118

12-
After removing the second node from the end, the linked list becomes 1->2->3->5.
19+
**Example 2:**
20+
21+
```
22+
Input: head = [1], n = 1
23+
Output: []
24+
1325
```
1426

27+
**Example 3:**
28+
29+
```
30+
Input: head = [1,2], n = 1
31+
Output: [1]
32+
33+
```
34+
35+
**Constraints:**
36+
37+
- The number of nodes in the list is `sz`.
38+
- `1 <= sz <= 30`
39+
- `0 <= Node.val <= 100`
40+
- `1 <= n <= sz`
41+
42+
1543
## 题目大意
1644

1745
删除链表中倒数第 n 个结点。

website/content/ChapterFour/0001~0099/0019.Remove-Nth-Node-From-End-of-List.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,43 @@
22

33
## 题目
44

5-
Given a linked list, remove the n-th node from the end of list and return its head.
5+
Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
66

7-
**Example**:
7+
**Follow up:** Could you do this in one pass?
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
12+
13+
```
14+
Input: head = [1,2,3,4,5], n = 2
15+
Output: [1,2,3,5]
16+
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: head = [1], n = 1
23+
Output: []
824
925
```
1026

11-
Given linked list: 1->2->3->4->5, and n = 2.
27+
**Example 3:**
1228

13-
After removing the second node from the end, the linked list becomes 1->2->3->5.
29+
```
30+
Input: head = [1,2], n = 1
31+
Output: [1]
1432
1533
```
1634

35+
**Constraints:**
36+
37+
- The number of nodes in the list is `sz`.
38+
- `1 <= sz <= 30`
39+
- `0 <= Node.val <= 100`
40+
- `1 <= n <= sz`
41+
1742
## 题目大意
1843

1944
删除链表中倒数第 n 个结点。
@@ -30,6 +55,13 @@ After removing the second node from the end, the linked list becomes 1->2->3->5.
3055

3156
package leetcode
3257

58+
import (
59+
"github.com/halfrost/LeetCode-Go/structures"
60+
)
61+
62+
// ListNode define
63+
type ListNode = structures.ListNode
64+
3365
/**
3466
* Definition for singly-linked list.
3567
* type ListNode struct {
@@ -40,13 +72,16 @@ package leetcode
4072

4173
// 解法一
4274
func removeNthFromEnd(head *ListNode, n int) *ListNode {
75+
if head == nil {
76+
return nil
77+
}
4378
var fast, slow *ListNode
4479
fast = head
4580
slow = head
4681
step := 0
4782
for i := 0; i < n; i++ {
4883
// n maybe much larger than length of linklist
49-
if fast.Next == nil && step != 0 && step < n-1 {
84+
if fast.Next == nil && step < n-1 {
5085
return head
5186
}
5287
fast = fast.Next
@@ -103,6 +138,7 @@ func removeNthFromEnd1(head *ListNode, n int) *ListNode {
103138
}
104139

105140

141+
106142
```
107143

108144

website/content/ChapterFour/0001~0099/0066.Plus-One.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,23 @@ You may assume the integer does not contain any leading zero, except the number
4141
package leetcode
4242

4343
func plusOne(digits []int) []int {
44-
if len(digits) == 0 {
45-
return []int{}
46-
}
47-
carry := 1
4844
for i := len(digits) - 1; i >= 0; i-- {
49-
if digits[i]+carry > 9 {
50-
digits[i] = 0
51-
carry = 1
52-
} else {
53-
digits[i] += carry
54-
carry = 0
55-
break
45+
digits[i]++
46+
if digits[i] != 10 {
47+
// no carry
48+
return digits
5649
}
50+
// carry
51+
digits[i] = 0
5752
}
58-
if digits[0] == 0 && carry == 1 {
59-
digits = append([]int{1}, digits...)
60-
}
53+
// all carry
54+
digits[0] = 1
55+
digits = append(digits, 0)
6156
return digits
6257
}
6358

6459

60+
6561
```
6662

6763

0 commit comments

Comments
 (0)