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

23. Merge k Sorted Lists #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 3 commits into master from merge-k-sorted-lists
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ continually updating 😃.
* [2. Add Two Numbers](./src/0002_add_two_numbers/add_two_numbers.go)   *`recursion;`*  *`math`*
* [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`*
* [21. Merge Two Sorted Lists](./src/0021_merge_two_sorted_lists/mergeTwoLists.go)
* [23. Merge k Sorted Lists](src/0023_merge_k_sorted_lists/mksl.go)   *`heap`*
* [25. Reverse Nodes in k-Group](./src/0025_reverse_nodes_in_k_group/reverse_node_k_group.go)
* [61. Rotate List](./src/0061_rotate_list/rotate_list.go)

Expand Down
92 changes: 92 additions & 0 deletions src/0023_merge_k_sorted_lists/mksl.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
23. Merge k Sorted Lists
https://leetcode.com/problems/merge-k-sorted-lists/

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
*/
// time: 2019年01月02日

package mksl

import (
"sort"
)

// ListNode Definition for singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}

// heap
// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.
// space complexity: O(n), where n is len of all lists.
func mergeKLists(lists []*ListNode) *ListNode {
for i := 0; i < len(lists); {
if lists[i] == nil {
lists = append(lists[0:i], lists[i+1:]...)
} else {
i++
}
}
if 0 == len(lists) {
return nil
}

head := &ListNode{}
cur := head
for len(lists) > 0 {
for i := (len(lists) - 1) / 2; i >= 0; i-- {
shiftUp(lists, len(lists), i)
}
cur.Next = &ListNode{Val: lists[0].Val}
cur = cur.Next
lists[0] = lists[0].Next
if lists[0] == nil {
lists = lists[1:]
}
}
return head.Next
}

// build heap
func shiftUp(lists []*ListNode, n int, k int) {
for 2*k+1 < n {
j := 2*k + 1
if j+1 < n && lists[j+1].Val < lists[j].Val {
j = j + 1
}
if lists[k].Val < lists[j].Val {
break
}

lists[j], lists[k] = lists[k], lists[j]
k = j
}
}

// brute force
// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.
// space complexity: O(2n), where n is len of all lists.
func mergeKLists1(lists []*ListNode) *ListNode {
nodes := make([]int, 0)
head := &ListNode{}
pointer := head

for _, j := range lists {
for {
if j != nil {
nodes = append(nodes, j.Val)
j = j.Next
} else {
break
}
}
}
sort.Ints(nodes)
for _, j := range nodes {
pointer.Next = &ListNode{Val: j}
pointer = pointer.Next
}
return head.Next
}
86 changes: 86 additions & 0 deletions src/0023_merge_k_sorted_lists/mksl_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package mksl

import (
"reflect"
"testing"
)

func TestMergeKLists(t *testing.T) {
testCases := [][]*ListNode{
{
createSingleList([]int{1, 4, 5}),
createSingleList([]int{2, 3, 4}),
createSingleList([]int{4, 6}),
},
{
createSingleList([]int{}),
createSingleList([]int{4, 5}),
createSingleList([]int{}),
},
{
createSingleList([]int{}),
createSingleList([]int{}),
createSingleList([]int{}),
},
{
createSingleList([]int{1, 4, 5}),
createSingleList([]int{1, 3, 4}),
createSingleList([]int{2, 6}),
},
}

expected := []*ListNode{
createSingleList([]int{1, 2, 3, 4, 4, 4, 5, 6}),
createSingleList([]int{4, 5}),
nil,
createSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),
}

for index, lists := range testCases {
if res := mergeKLists(lists); !reflect.DeepEqual(res, expected[index]) {
t.Errorf("expected %v, got %v", expected[index], res)
}
}
}

func TestMergeKLists1(t *testing.T) {
testCases := [][]*ListNode{
{
createSingleList([]int{1, 4, 5}),
createSingleList([]int{1, 3, 4}),
createSingleList([]int{2, 6}),
},
{
createSingleList([]int{}),
createSingleList([]int{4, 5}),
createSingleList([]int{}),
},
{
createSingleList([]int{}),
createSingleList([]int{}),
createSingleList([]int{}),
},
}

expected := []*ListNode{
createSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),
createSingleList([]int{4, 5}),
nil,
}

for index, lists := range testCases {
if res := mergeKLists1(lists); !reflect.DeepEqual(res, expected[index]) {
t.Errorf("expected %v, got %v", expected[index], res)
}
}
}

func createSingleList(nums []int) *ListNode {
head := &ListNode{}
cur := head
for _, j := range nums {
cur.Next = &ListNode{Val: j}
cur = cur.Next
}
return head.Next
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
|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`*|
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
|0023|[23. Merge k Sorted Lists](0023_merge_k_sorted_lists/mksl.go)|Hard|*`linked list;`**`heap`*|
|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|
|0026|[Remove Duplicates from Sorted Array](0026_remove_duplicates_from_sorted_array/rdfsa.go)|Easy|*`array;`* *`double index`*|
|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|
Expand Down

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