Skip to content

Commit 5f11276

Browse files
aQuaaQua
aQua
authored and
aQua
committed
kit 把 ListNode 的处理函数,变成公开的。
1 parent 0c5f250 commit 5f11276

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

kit/ListNode.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ type ListNode struct {
1111
Next *ListNode
1212
}
1313

14-
// convert *ListNode to []int
15-
func l2s(head *ListNode) []int {
14+
// List2Slice convert List to []int
15+
func List2Slice(head *ListNode) []int {
1616
// 链条深度限制,链条深度超出此限制,会 panic
1717
limit := 100
18+
1819
times := 0
1920

2021
res := []int{}
@@ -32,15 +33,16 @@ func l2s(head *ListNode) []int {
3233
return res
3334
}
3435

35-
// convert []int to *ListNode
36-
func s2l(nums []int) *ListNode {
36+
// Slice2List convert []int to List
37+
func Slice2List(nums []int) *ListNode {
3738
if len(nums) == 0 {
3839
return nil
3940
}
4041

4142
res := &ListNode{
4243
Val: nums[0],
4344
}
45+
4446
temp := res
4547
for i := 1; i < len(nums); i++ {
4648
temp.Next = &ListNode{

kit/ListNode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func Test_l2s(t *testing.T) {
1010
ast := assert.New(t)
11-
ast.Equal([]int{}, l2s(nil), "输入nil,没有返回[]int{}")
11+
ast.Equal([]int{}, List2Slice(nil), "输入nil,没有返回[]int{}")
1212

1313
one2three := &ListNode{
1414
Val: 1,
@@ -19,18 +19,18 @@ func Test_l2s(t *testing.T) {
1919
},
2020
},
2121
}
22-
ast.Equal([]int{1, 2, 3}, l2s(one2three), "没有成功地转换成[]int")
22+
ast.Equal([]int{1, 2, 3}, List2Slice(one2three), "没有成功地转换成[]int")
2323

2424
limit := 100
25-
overLimitList := s2l(make([]int, limit+1))
26-
ast.Panics(func() { l2s(overLimitList) }, "转换深度超过 %d 限制的链条,没有 panic", limit)
25+
overLimitList := Slice2List(make([]int, limit+1))
26+
ast.Panics(func() { List2Slice(overLimitList) }, "转换深度超过 %d 限制的链条,没有 panic", limit)
2727
}
2828

2929
func Test_s2l(t *testing.T) {
3030
ast := assert.New(t)
31-
ast.Nil(s2l([]int{}), "输入[]int{},没有返回nil")
31+
ast.Nil(Slice2List([]int{}), "输入[]int{},没有返回nil")
3232

33-
ln := s2l([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
33+
ln := Slice2List([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
3434
i := 1
3535
for ln != nil {
3636
ast.Equal(i, ln.Val, "对应的值不对")

0 commit comments

Comments
 (0)