-
Notifications
You must be signed in to change notification settings - Fork 47
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
src/0023_merge_k_sorted_lists/mksl.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.