From b743314597a1f55387e4bbd106daf1cb7973ac44 Mon Sep 17 00:00:00 2001 From: kimiimac Date: Mon, 4 Mar 2024 23:20:07 +0800 Subject: [PATCH] feat: 19.Renove Nth Node from end of list --- Leetcode/0019.Remove-Nth-Node-From-End-of-List/README.md | 6 +++++- Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go | 4 ++++ README.md | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) 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 60d19e951..6e7e4625a 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 @@ -4,7 +4,7 @@ tags: Medium, Two Pointers author: Kimi Tsai description: --- -# [19. Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/) +# [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) ## 題目 Given the head of a linked list, remove the nth node from the end of the list and return its head. @@ -65,6 +65,10 @@ type ListNode struct { Next *ListNode } +// 產生 dummyHead,跟 preslow +// 使用雙指針, 先讓 fast走 `k` 步, 然後 `fast slow 同速前進` +// 這樣當fast走到nil時, slow所在位置就是在倒數第 k 的節點 +// 將 slow的前一步(preslow)的next 指向 slow.Next func RemoveNthFromEnd(head *ListNode, n int) *ListNode { dummyHead := &ListNode{Next: head} preSlow, slow, fast := dummyHead, head, head diff --git a/Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go b/Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go index df3d4aef4..65fef229e 100644 --- a/Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go +++ b/Leetcode/0019.Remove-Nth-Node-From-End-of-List/main.go @@ -13,6 +13,10 @@ type ListNode struct { Next *ListNode } +// 產生 dummyHead,跟 preslow +// 使用雙指針, 先讓 fast走 `k` 步, 然後 `fast slow 同速前進` +// 這樣當fast走到nil時, slow所在位置就是在倒數第 k 的節點 +// 將 slow的前一步(preslow)的next 指向 slow.Next func RemoveNthFromEnd(head *ListNode, n int) *ListNode { dummyHead := &ListNode{Next: head} preSlow, slow, fast := dummyHead, head, head diff --git a/README.md b/README.md index bfa3abb98..0323bfb7c 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ https://kimi0230.github.io/LeetcodeGolang/ | No. | Title | Solution | Difficulty | Time | Space | Topic | |---------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------:|------------|-------------|-------|---------------------------| -| [0019](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0019.Remove-Nth-Node-From-End-of-List/) | [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 | +| [0019](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0019.Remove-Nth-Node-From-End-of-List/) | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-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 | | [0141](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0141.Linked-List-Cycle/) | [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 | | [0142](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0142.Linked-List-CycleII/) | [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 | | [0203](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0203.Remove-Linked-List-Elements) | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0203.Remove-Linked-List-Elements) | Easy | O(n) | O(1) | Linked List |