Update 0019、0066

This commit is contained in:
YDZ
2021-04-16 00:12:51 +08:00
parent 7f0a3b514f
commit 86fac4e139
3 changed files with 84 additions and 24 deletions

View File

@ -2,16 +2,44 @@
## 题目 ## 题目
Given a linked list, remove the n-th node from the end of list and return its head. Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
Example: **Follow up:** Could you do this in one pass?
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
``` ```
Given linked list: 1->2->3->4->5, and n = 2. Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
After removing the second node from the end, the linked list becomes 1->2->3->5.
``` ```
**Example 2:**
```
Input: head = [1], n = 1
Output: []
```
**Example 3:**
```
Input: head = [1,2], n = 1
Output: [1]
```
**Constraints:**
- The number of nodes in the list is `sz`.
- `1 <= sz <= 30`
- `0 <= Node.val <= 100`
- `1 <= n <= sz`
## 题目大意 ## 题目大意
删除链表中倒数第 n 个结点。 删除链表中倒数第 n 个结点。

View File

@ -2,17 +2,42 @@
## 题目 ## 题目
Given a linked list, remove the n-th node from the end of list and return its head. Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
**Example**: **Follow up:** Could you do this in one pass?
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
```
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
``` ```
Given linked list: 1->2->3->4->5, and n = 2. **Example 2:**
After removing the second node from the end, the linked list becomes 1->2->3->5.
``` ```
Input: head = [1], n = 1
Output: []
```
**Example 3:**
```
Input: head = [1,2], n = 1
Output: [1]
```
**Constraints:**
- The number of nodes in the list is `sz`.
- `1 <= sz <= 30`
- `0 <= Node.val <= 100`
- `1 <= n <= sz`
## 题目大意 ## 题目大意
@ -30,6 +55,13 @@ After removing the second node from the end, the linked list becomes 1->2->3->5.
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {
@ -40,13 +72,16 @@ package leetcode
// 解法一 // 解法一
func removeNthFromEnd(head *ListNode, n int) *ListNode { func removeNthFromEnd(head *ListNode, n int) *ListNode {
if head == nil {
return nil
}
var fast, slow *ListNode var fast, slow *ListNode
fast = head fast = head
slow = head slow = head
step := 0 step := 0
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
// n maybe much larger than length of linklist // n maybe much larger than length of linklist
if fast.Next == nil && step != 0 && step < n-1 { if fast.Next == nil && step < n-1 {
return head return head
} }
fast = fast.Next fast = fast.Next
@ -103,6 +138,7 @@ func removeNthFromEnd1(head *ListNode, n int) *ListNode {
} }
``` ```

View File

@ -41,27 +41,23 @@ You may assume the integer does not contain any leading zero, except the number
package leetcode package leetcode
func plusOne(digits []int) []int { func plusOne(digits []int) []int {
if len(digits) == 0 {
return []int{}
}
carry := 1
for i := len(digits) - 1; i >= 0; i-- { for i := len(digits) - 1; i >= 0; i-- {
if digits[i]+carry > 9 { digits[i]++
digits[i] = 0 if digits[i] != 10 {
carry = 1 // no carry
} else { return digits
digits[i] += carry
carry = 0
break
} }
// carry
digits[i] = 0
} }
if digits[0] == 0 && carry == 1 { // all carry
digits = append([]int{1}, digits...) digits[0] = 1
} digits = append(digits, 0)
return digits return digits
} }
``` ```