Documentation
¶
Index ¶
- type ListNode
- func MergeKLists(lists []*ListNode) *ListNode
- func MergeTwoSortedLists(l1 *ListNode, l2 *ListNode) *ListNode
- func RemoveNthFromEnd(head *ListNode, n int) *ListNode
- func RemoveNthFromEnd1(head *ListNode, n int) *ListNode
- func ReverseKGroup(head *ListNode, k int) *ListNode
- func RotateRight(head *ListNode, k int) *ListNode
- func SwapPairs(head *ListNode) *ListNode
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ListNode ¶
type ListNode = structures.ListNode
ListNode define
func MergeKLists ¶
*
- Definition for singly-linked list.
- type ListNode struct {
- Val int
- Next *ListNode
- }
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question23 struct { para23 ans23 } // para 是参数 // one 代表第一个参数 type para23 struct { one [][]int } // ans 是答案 // one 代表第一个答案 type ans23 struct { one []int } func main() { qs := []question23{ { para23{[][]int{}}, ans23{[]int{}}, }, { para23{[][]int{ {1}, {1}, }}, ans23{[]int{1, 1}}, }, { para23{[][]int{ {1, 2, 3, 4}, {1, 2, 3, 4}, }}, ans23{[]int{1, 1, 2, 2, 3, 3, 4, 4}}, }, { para23{[][]int{ {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, }}, ans23{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}}, }, { para23{[][]int{ {1}, {9, 9, 9, 9, 9}, }}, ans23{[]int{1, 9, 9, 9, 9, 9}}, }, { para23{[][]int{ {9, 9, 9, 9, 9}, {1}, }}, ans23{[]int{1, 9, 9, 9, 9, 9}}, }, { para23{[][]int{ {2, 3, 4}, {4, 5, 6}, {7, 8, 9}, }}, ans23{[]int{2, 3, 4, 4, 5, 6, 7, 8, 9}}, }, { para23{[][]int{ {1, 3, 8}, {1, 7}, }}, ans23{[]int{1, 1, 3, 7, 8}}, }, // 如需多个测试,可以复制上方元素。 } for _, q := range qs { var ls []*ListNode for _, qq := range q.para23.one { ls = append(ls, structures.Ints2List(qq)) } fmt.Println(q.para23.one, structures.List2Ints(MergeKLists(ls))) } }
Output: [] [] [[1] [1]] [1 1] [[1 2 3 4] [1 2 3 4]] [1 1 2 2 3 3 4 4] [[1 2 3 4 5] [1 2 3 4 5]] [1 1 2 2 3 3 4 4 5 5] [[1] [9 9 9 9 9]] [1 9 9 9 9 9] [[9 9 9 9 9] [1]] [1 9 9 9 9 9] [[2 3 4] [4 5 6] [7 8 9]] [2 3 4 4 5 6 7 8 9] [[1 3 8] [1 7]] [1 1 3 7 8]
func MergeTwoSortedLists ¶
*
- Definition for singly-linked list.
- type ListNode struct {
- Val int
- Next *ListNode
- }
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question21 struct { para21 ans21 } // para 是参数 // one 代表第一个参数 type para21 struct { one []int another []int } // ans 是答案 // one 代表第一个答案 type ans21 struct { one []int } func main() { qs := []question21{ { para21{[]int{}, []int{}}, ans21{[]int{}}, }, { para21{[]int{1}, []int{1}}, ans21{[]int{1, 1}}, }, { para21{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4}}, }, { para21{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}}, }, { para21{[]int{1}, []int{9, 9, 9, 9, 9}}, ans21{[]int{1, 9, 9, 9, 9, 9}}, }, { para21{[]int{9, 9, 9, 9, 9}, []int{1}}, ans21{[]int{1, 9, 9, 9, 9, 9}}, }, { para21{[]int{2, 3, 4}, []int{4, 5, 6}}, ans21{[]int{2, 3, 4, 4, 5, 6}}, }, { para21{[]int{1, 3, 8}, []int{1, 7}}, ans21{[]int{1, 1, 3, 7, 8}}, }, // 如需多个测试,可以复制上方元素。 } for _, q := range qs { _, p := q.ans21, q.para21 fmt.Println(p, structures.List2Ints(MergeTwoSortedLists(structures.Ints2List(p.one), structures.Ints2List(p.another)))) } }
Output: {[] []} [] {[1] [1]} [1 1] {[1 2 3 4] [1 2 3 4]} [1 1 2 2 3 3 4 4] {[1 2 3 4 5] [1 2 3 4 5]} [1 1 2 2 3 3 4 4 5 5] {[1] [9 9 9 9 9]} [1 9 9 9 9 9] {[9 9 9 9 9] [1]} [1 9 9 9 9 9] {[2 3 4] [4 5 6]} [2 3 4 4 5 6] {[1 3 8] [1 7]} [1 1 3 7 8]
func RemoveNthFromEnd ¶
解法一
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question19 struct { para19 ans19 } // para 是参数 // one 代表第一个参数 type para19 struct { one []int n int } // ans 是答案 // one 代表第一个答案 type ans19 struct { one []int } func main() { qs := []question19{ { para19{[]int{1}, 3}, ans19{[]int{1}}, }, { para19{[]int{1, 2}, 2}, ans19{[]int{2}}, }, { para19{[]int{1}, 1}, ans19{[]int{}}, }, { para19{[]int{1, 2, 3, 4, 5}, 10}, ans19{[]int{1, 2, 3, 4, 5}}, }, { para19{[]int{1, 2, 3, 4, 5}, 1}, ans19{[]int{1, 2, 3, 4}}, }, { para19{[]int{1, 2, 3, 4, 5}, 2}, ans19{[]int{1, 2, 3, 5}}, }, { para19{[]int{1, 2, 3, 4, 5}, 3}, ans19{[]int{1, 2, 4, 5}}, }, { para19{[]int{1, 2, 3, 4, 5}, 4}, ans19{[]int{1, 3, 4, 5}}, }, { para19{[]int{1, 2, 3, 4, 5}, 5}, ans19{[]int{2, 3, 4, 5}}, }, } for _, q := range qs { _, p := q.ans19, q.para19 fmt.Println(p, structures.List2Ints(RemoveNthFromEnd(structures.Ints2List(p.one), p.n))) } }
Output: {[1] 3} [] {[1 2] 2} [2] {[1] 1} [] {[1 2 3 4 5] 10} [2 3 4 5] {[1 2 3 4 5] 1} [1 2 3 4] {[1 2 3 4 5] 2} [1 2 3 5] {[1 2 3 4 5] 3} [1 2 4 5] {[1 2 3 4 5] 4} [1 3 4 5] {[1 2 3 4 5] 5} [2 3 4 5]
func ReverseKGroup ¶
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question25 struct { para25 ans25 } // para 是参数 // one 代表第一个参数 type para25 struct { one []int two int } // ans 是答案 // one 代表第一个答案 type ans25 struct { one []int } func main() { qs := []question25{ { para25{ []int{1, 2, 3, 4, 5}, 3, }, ans25{[]int{3, 2, 1, 4, 5}}, }, { para25{ []int{1, 2, 3, 4, 5}, 1, }, ans25{[]int{1, 2, 3, 4, 5}}, }, } for _, q := range qs { _, p := q.ans25, q.para25 fmt.Println(p, structures.List2Ints(ReverseKGroup(structures.Ints2List(p.one), p.two))) } }
Output: {[1 2 3 4 5] 3} [3 2 1 4 5] {[1 2 3 4 5] 1} [1 2 3 4 5]
func RotateRight ¶
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question61 struct { para61 ans61 } // para 是参数 // one 代表第一个参数 type para61 struct { one []int k int } // ans 是答案 // one 代表第一个答案 type ans61 struct { one []int } func main() { qs := []question61{ { para61{[]int{1, 2, 3, 4, 5}, 2}, ans61{[]int{4, 5, 1, 2, 3}}, }, { para61{[]int{1, 2, 3, 4, 5}, 3}, ans61{[]int{4, 5, 1, 2, 3}}, }, { para61{[]int{0, 1, 2}, 4}, ans61{[]int{2, 0, 1}}, }, { para61{[]int{1, 1, 1, 2}, 3}, ans61{[]int{1, 1, 2, 1}}, }, { para61{[]int{1}, 10}, ans61{[]int{1}}, }, { para61{[]int{}, 100}, ans61{[]int{}}, }, } for _, q := range qs { _, p := q.ans61, q.para61 fmt.Println(p, structures.List2Ints(RotateRight(structures.Ints2List(p.one), p.k))) } }
Output: {[1 2 3 4 5] 2} [4 5 1 2 3] {[1 2 3 4 5] 3} [3 4 5 1 2] {[0 1 2] 4} [2 0 1] {[1 1 1 2] 3} [1 1 2 1] {[1] 10} [1] {[] 100} []
func SwapPairs ¶
Example ¶
package main import ( "fmt" "github.com/gopperin/leetcode/structures" ) type question24 struct { para24 ans24 } // para 是参数 // one 代表第一个参数 type para24 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans24 struct { one []int } func main() { qs := []question24{ { para24{[]int{}}, ans24{[]int{}}, }, { para24{[]int{1}}, ans24{[]int{1}}, }, { para24{[]int{1, 2, 3, 4}}, ans24{[]int{2, 1, 4, 3}}, }, { para24{[]int{1, 2, 3, 4, 5}}, ans24{[]int{2, 1, 4, 3, 5}}, }, // 如需多个测试,可以复制上方元素。 } for _, q := range qs { _, p := q.ans24, q.para24 fmt.Println(p, structures.List2Ints(SwapPairs(structures.Ints2List(p.one)))) } }
Output: {[]} [] {[1]} [1] {[1 2 3 4]} [2 1 4 3] {[1 2 3 4 5]} [2 1 4 3 5]
Click to show internal directories.
Click to hide internal directories.