Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 93076b5

Browse files
authored
23. Merge k Sorted Lists (#31)
* 23 solved. * add more ut case for 23 * add more ut case for 23
1 parent a4bd398 commit 93076b5

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ continually updating 😃.
5151
* [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)   *`recursion;`*  *`math`*
5252
* [19. Remove Nth Node From End of List](src/0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go)   *`two pointers`*
5353
* [21. Merge Two Sorted Lists](./src/0021_merge_two_sorted_lists/mergeTwoLists.go)
54+
* [23. Merge k Sorted Lists](src/0023_merge_k_sorted_lists/mksl.go)   *`heap`*
5455
* [25. Reverse Nodes in k-Group](./src/0025_reverse_nodes_in_k_group/reverse_node_k_group.go)
5556
* [61. Rotate List](./src/0061_rotate_list/rotate_list.go)
5657

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
23. Merge k Sorted Lists
3+
https://leetcode.com/problems/merge-k-sorted-lists/
4+
5+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
6+
*/
7+
// time: 2019年01月02日
8+
9+
package mksl
10+
11+
import (
12+
"sort"
13+
)
14+
15+
// ListNode Definition for singly-linked list.
16+
type ListNode struct {
17+
Val int
18+
Next *ListNode
19+
}
20+
21+
// heap
22+
// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.
23+
// space complexity: O(n), where n is len of all lists.
24+
func mergeKLists(lists []*ListNode) *ListNode {
25+
for i := 0; i < len(lists); {
26+
if lists[i] == nil {
27+
lists = append(lists[0:i], lists[i+1:]...)
28+
} else {
29+
i++
30+
}
31+
}
32+
if 0 == len(lists) {
33+
return nil
34+
}
35+
36+
head := &ListNode{}
37+
cur := head
38+
for len(lists) > 0 {
39+
for i := (len(lists) - 1) / 2; i >= 0; i-- {
40+
shiftUp(lists, len(lists), i)
41+
}
42+
cur.Next = &ListNode{Val: lists[0].Val}
43+
cur = cur.Next
44+
lists[0] = lists[0].Next
45+
if lists[0] == nil {
46+
lists = lists[1:]
47+
}
48+
}
49+
return head.Next
50+
}
51+
52+
// build heap
53+
func shiftUp(lists []*ListNode, n int, k int) {
54+
for 2*k+1 < n {
55+
j := 2*k + 1
56+
if j+1 < n && lists[j+1].Val < lists[j].Val {
57+
j = j + 1
58+
}
59+
if lists[k].Val < lists[j].Val {
60+
break
61+
}
62+
63+
lists[j], lists[k] = lists[k], lists[j]
64+
k = j
65+
}
66+
}
67+
68+
// brute force
69+
// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.
70+
// space complexity: O(2n), where n is len of all lists.
71+
func mergeKLists1(lists []*ListNode) *ListNode {
72+
nodes := make([]int, 0)
73+
head := &ListNode{}
74+
pointer := head
75+
76+
for _, j := range lists {
77+
for {
78+
if j != nil {
79+
nodes = append(nodes, j.Val)
80+
j = j.Next
81+
} else {
82+
break
83+
}
84+
}
85+
}
86+
sort.Ints(nodes)
87+
for _, j := range nodes {
88+
pointer.Next = &ListNode{Val: j}
89+
pointer = pointer.Next
90+
}
91+
return head.Next
92+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package mksl
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestMergeKLists(t *testing.T) {
9+
testCases := [][]*ListNode{
10+
{
11+
createSingleList([]int{1, 4, 5}),
12+
createSingleList([]int{2, 3, 4}),
13+
createSingleList([]int{4, 6}),
14+
},
15+
{
16+
createSingleList([]int{}),
17+
createSingleList([]int{4, 5}),
18+
createSingleList([]int{}),
19+
},
20+
{
21+
createSingleList([]int{}),
22+
createSingleList([]int{}),
23+
createSingleList([]int{}),
24+
},
25+
{
26+
createSingleList([]int{1, 4, 5}),
27+
createSingleList([]int{1, 3, 4}),
28+
createSingleList([]int{2, 6}),
29+
},
30+
}
31+
32+
expected := []*ListNode{
33+
createSingleList([]int{1, 2, 3, 4, 4, 4, 5, 6}),
34+
createSingleList([]int{4, 5}),
35+
nil,
36+
createSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),
37+
}
38+
39+
for index, lists := range testCases {
40+
if res := mergeKLists(lists); !reflect.DeepEqual(res, expected[index]) {
41+
t.Errorf("expected %v, got %v", expected[index], res)
42+
}
43+
}
44+
}
45+
46+
func TestMergeKLists1(t *testing.T) {
47+
testCases := [][]*ListNode{
48+
{
49+
createSingleList([]int{1, 4, 5}),
50+
createSingleList([]int{1, 3, 4}),
51+
createSingleList([]int{2, 6}),
52+
},
53+
{
54+
createSingleList([]int{}),
55+
createSingleList([]int{4, 5}),
56+
createSingleList([]int{}),
57+
},
58+
{
59+
createSingleList([]int{}),
60+
createSingleList([]int{}),
61+
createSingleList([]int{}),
62+
},
63+
}
64+
65+
expected := []*ListNode{
66+
createSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),
67+
createSingleList([]int{4, 5}),
68+
nil,
69+
}
70+
71+
for index, lists := range testCases {
72+
if res := mergeKLists1(lists); !reflect.DeepEqual(res, expected[index]) {
73+
t.Errorf("expected %v, got %v", expected[index], res)
74+
}
75+
}
76+
}
77+
78+
func createSingleList(nums []int) *ListNode {
79+
head := &ListNode{}
80+
cur := head
81+
for _, j := range nums {
82+
cur.Next = &ListNode{Val: j}
83+
cur = cur.Next
84+
}
85+
return head.Next
86+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
|0019|[19. Remove Nth Node From End of List](0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go)|Medium|*`linked list`*|
1616
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
1717
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
18+
|0023|[23. Merge k Sorted Lists](0023_merge_k_sorted_lists/mksl.go)|Hard|*`linked list;`**`heap`*|
1819
|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|
1920
|0026|[Remove Duplicates from Sorted Array](0026_remove_duplicates_from_sorted_array/rdfsa.go)|Easy|*`array;`* *`double index`*|
2021
|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /