diff --git a/leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md b/leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md index 807a6cbc..090e74ef 100644 --- a/leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md +++ b/leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md @@ -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 个结点。 diff --git a/website/content/ChapterFour/0001~0099/0019.Remove-Nth-Node-From-End-of-List.md b/website/content/ChapterFour/0001~0099/0019.Remove-Nth-Node-From-End-of-List.md index d4e38560..e752190a 100644 --- a/website/content/ChapterFour/0001~0099/0019.Remove-Nth-Node-From-End-of-List.md +++ b/website/content/ChapterFour/0001~0099/0019.Remove-Nth-Node-From-End-of-List.md @@ -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. - -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` ## 题目大意 @@ -30,6 +55,13 @@ After removing the second node from the end, the linked list becomes 1->2->3->5. package leetcode +import ( + "github.com/halfrost/LeetCode-Go/structures" +) + +// ListNode define +type ListNode = structures.ListNode + /** * Definition for singly-linked list. * type ListNode struct { @@ -40,13 +72,16 @@ package leetcode // 解法一 func removeNthFromEnd(head *ListNode, n int) *ListNode { + if head == nil { + return nil + } var fast, slow *ListNode fast = head slow = head step := 0 for i := 0; i < n; i++ { // 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 } fast = fast.Next @@ -103,6 +138,7 @@ func removeNthFromEnd1(head *ListNode, n int) *ListNode { } + ``` diff --git a/website/content/ChapterFour/0001~0099/0066.Plus-One.md b/website/content/ChapterFour/0001~0099/0066.Plus-One.md index 56110cb6..115a610c 100755 --- a/website/content/ChapterFour/0001~0099/0066.Plus-One.md +++ b/website/content/ChapterFour/0001~0099/0066.Plus-One.md @@ -41,27 +41,23 @@ You may assume the integer does not contain any leading zero, except the number package leetcode func plusOne(digits []int) []int { - if len(digits) == 0 { - return []int{} - } - carry := 1 for i := len(digits) - 1; i >= 0; i-- { - if digits[i]+carry > 9 { - digits[i] = 0 - carry = 1 - } else { - digits[i] += carry - carry = 0 - break + digits[i]++ + if digits[i] != 10 { + // no carry + return digits } + // carry + digits[i] = 0 } - if digits[0] == 0 && carry == 1 { - digits = append([]int{1}, digits...) - } + // all carry + digits[0] = 1 + digits = append(digits, 0) return digits } + ```