mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 18:10:29 +08:00
添加 problem 19
This commit is contained in:
17
Algorithms/19.Remove Nth Node From End of List/README.md
Normal file
17
Algorithms/19.Remove Nth Node From End of List/README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
|
||||||
|
|
||||||
|
## 题目
|
||||||
|
|
||||||
|
Given a linked list, remove the n-th node from the end of list and return its head.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
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.
|
||||||
|
```
|
||||||
|
|
||||||
|
## 题目大意
|
||||||
|
|
||||||
|
删除链表中倒数第 n 个结点。这道题比较简单,先循环一次拿到链表的总长度,然后循环到要删除的结点的前一个结点开始删除操作。需要注意的一个特例是,有可能要删除头结点,要单独处理。
|
@ -0,0 +1,45 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* type ListNode struct {
|
||||||
|
* Val int
|
||||||
|
* Next *ListNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||||
|
if head == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if n <= 0 {
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
current := head
|
||||||
|
len := 0
|
||||||
|
for current != nil {
|
||||||
|
len++
|
||||||
|
current = current.Next
|
||||||
|
}
|
||||||
|
if n > len {
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
if n == len {
|
||||||
|
current := head
|
||||||
|
head = head.Next
|
||||||
|
current.Next = nil
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
current = head
|
||||||
|
i := 0
|
||||||
|
for current != nil {
|
||||||
|
if i == len-n-1 {
|
||||||
|
deleteNode := current.Next
|
||||||
|
current.Next = current.Next.Next
|
||||||
|
deleteNode.Next = nil
|
||||||
|
break
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
current = current.Next
|
||||||
|
}
|
||||||
|
return head
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question19 struct {
|
||||||
|
para19
|
||||||
|
ans19
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para19 struct {
|
||||||
|
one []int
|
||||||
|
n int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans19 struct {
|
||||||
|
one []int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem19(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question19{
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 1},
|
||||||
|
ans19{[]int{1, 2, 3, 4}},
|
||||||
|
},
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 2},
|
||||||
|
ans19{[]int{1, 2, 3, 5}},
|
||||||
|
},
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 3},
|
||||||
|
ans19{[]int{1, 2, 4, 5}},
|
||||||
|
},
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 4},
|
||||||
|
ans19{[]int{1, 3, 4, 5}},
|
||||||
|
},
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 5},
|
||||||
|
ans19{[]int{2, 3, 4, 5}},
|
||||||
|
},
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 0},
|
||||||
|
ans19{[]int{1, 2, 3, 4, 5}},
|
||||||
|
},
|
||||||
|
|
||||||
|
question19{
|
||||||
|
para19{[]int{1, 2, 3, 4, 5}, 10},
|
||||||
|
ans19{[]int{1, 2, 3, 4, 5}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 19------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans19, q.para19
|
||||||
|
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(removeNthFromEnd(S2l(p.one), p.n)))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
@ -77,35 +77,3 @@ func Test_Problem2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
fmt.Printf("\n\n\n")
|
fmt.Printf("\n\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// // convert *ListNode to []int
|
|
||||||
// func L2s(head *ListNode) []int {
|
|
||||||
// res := []int{}
|
|
||||||
|
|
||||||
// for head != nil {
|
|
||||||
// res = append(res, head.Val)
|
|
||||||
// head = head.Next
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return res
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // convert []int to *ListNode
|
|
||||||
// func S2l(nums []int) *ListNode {
|
|
||||||
// if len(nums) == 0 {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// res := &ListNode{
|
|
||||||
// Val: nums[0],
|
|
||||||
// }
|
|
||||||
// temp := res
|
|
||||||
// for i := 1; i < len(nums); i++ {
|
|
||||||
// temp.Next = &ListNode{
|
|
||||||
// Val: nums[i],
|
|
||||||
// }
|
|
||||||
// temp = temp.Next
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return res
|
|
||||||
// }
|
|
||||||
|
Reference in New Issue
Block a user