Skip to content

Commit 52deeed

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 23
按照合并两个ListNode的方法,合并多个ListNode。但是效率不高
1 parent f4550e7 commit 52deeed

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

Algorithms/0023.merge-k-sorted-lists/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)
22

33
## 题目
4-
4+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
55

66
## 解题思路
77

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
package Problem0023
22

3+
// ListNode 是链接节点
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func mergeKLists(lists []*ListNode) *ListNode {
10+
if len(lists) == 0 {
11+
return nil
12+
}
13+
14+
res := lists[0]
15+
16+
for i := 1; i < len(lists); i++ {
17+
res = merge2Lists(res, lists[i])
18+
}
19+
20+
return res
21+
}
22+
23+
func merge2Lists(l1 *ListNode, l2 *ListNode) *ListNode {
24+
// 有一条链为nil,直接返回另一条链
25+
if l1 == nil {
26+
return l2
27+
}
28+
if l2 == nil {
29+
return l1
30+
}
31+
32+
// 此时,两条链都不为nil,可以直接使用l.Val,而不用担心panic
33+
// 在l1和l2之间,选择较小的节点作为head,并设置好node
34+
var head, node *ListNode
35+
if l1.Val < l2.Val {
36+
head = l1
37+
node = l1
38+
l1 = l1.Next
39+
} else {
40+
head = l2
41+
node = l2
42+
l2 = l2.Next
43+
}
44+
45+
// 循环比较l1和l2的值,始终选择较小的值连上node
46+
for l1 != nil && l2 != nil {
47+
if l1.Val < l2.Val {
48+
node.Next = l1
49+
l1 = l1.Next
50+
} else {
51+
node.Next = l2
52+
l2 = l2.Next
53+
}
54+
55+
// 有了这一步,head才是一个完整的链
56+
node = node.Next
57+
}
58+
59+
if l1 != nil {
60+
// 连上l1剩余的链
61+
node.Next = l1
62+
}
63+
64+
if l2 != nil {
65+
// 连上l2剩余的链
66+
node.Next = l2
67+
}
68+
69+
return head
70+
}
Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0023
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,13 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one [][]int
1919
}
2020

2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans struct {
24-
one string
24+
one []int
2525
}
2626

2727
func Test_Problem0023(t *testing.T) {
@@ -30,17 +30,72 @@ func Test_Problem0023(t *testing.T) {
3030
qs := []question{
3131

3232
question{
33-
para{""},
34-
ans{""},
33+
para{[][]int{
34+
[]int{1, 4, 7},
35+
[]int{2, 5, 8},
36+
[]int{3, 6, 9},
37+
}},
38+
ans{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9}},
39+
},
40+
41+
question{
42+
para{[][]int{
43+
[]int{1, 4, 7},
44+
[]int{2, 5, 8},
45+
[]int{},
46+
}},
47+
ans{[]int{1, 2, 4, 5, 7, 8}},
3548
},
36-
49+
3750
// 如需多个测试,可以复制上方元素。
3851
}
3952

4053
for _, q := range qs {
4154
a, p := q.ans, q.para
4255
fmt.Printf("~~%v~~\n", p)
4356

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
57+
ast.Equal(a.one, l2s(mergeKLists(ss2ls(p.one))), "输入:%v", p)
4558
}
4659
}
60+
61+
// convert *ListNode to []int
62+
func l2s(head *ListNode) []int {
63+
res := []int{}
64+
65+
for head != nil {
66+
res = append(res, head.Val)
67+
head = head.Next
68+
}
69+
70+
return res
71+
}
72+
73+
// convert []int to *ListNode
74+
func s2l(nums []int) *ListNode {
75+
if len(nums) == 0 {
76+
return nil
77+
}
78+
79+
res := &ListNode{
80+
Val: nums[0],
81+
}
82+
temp := res
83+
for i := 1; i < len(nums); i++ {
84+
temp.Next = &ListNode{
85+
Val: nums[i],
86+
}
87+
temp = temp.Next
88+
}
89+
90+
return res
91+
}
92+
93+
func ss2ls(numss [][]int) []*ListNode {
94+
res := []*ListNode{}
95+
96+
for _, nums := range numss {
97+
res = append(res, s2l(nums))
98+
}
99+
100+
return res
101+
}

kit/ListNode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type ListNode struct {
99

1010
// convert *ListNode to []int
1111
func l2s(head *ListNode) []int {
12-
var res []int
12+
res := []int{}
1313

1414
for head != nil {
1515
res = append(res, head.Val)

kit/ListNode_test.go

Lines changed: 1 addition & 1 deletion
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.Nil(l2s(nil), "输入nil,没有返回nil")
11+
ast.Equal([]int{}, l2s(nil), "输入nil,没有返回[]int{}")
1212

1313
one2three := &ListNode{
1414
Val: 1,

0 commit comments

Comments
 (0)