diff --git a/Algorithms/19.Remove Nth Node From End of List/README.md b/Algorithms/19.Remove Nth Node From End of List/README.md new file mode 100644 index 00000000..79d1ba68 --- /dev/null +++ b/Algorithms/19.Remove Nth Node From End of List/README.md @@ -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 个结点。这道题比较简单,先循环一次拿到链表的总长度,然后循环到要删除的结点的前一个结点开始删除操作。需要注意的一个特例是,有可能要删除头结点,要单独处理。 diff --git a/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List.go b/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List.go new file mode 100644 index 00000000..3effbfcb --- /dev/null +++ b/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List.go @@ -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 +} diff --git a/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List_test.go b/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List_test.go new file mode 100644 index 00000000..b8303f0f --- /dev/null +++ b/Algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List_test.go @@ -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") +} diff --git a/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go b/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go index 24d9d9fd..304ffa76 100644 --- a/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go +++ b/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go @@ -77,35 +77,3 @@ func Test_Problem2(t *testing.T) { } 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 -// }