-
Notifications
You must be signed in to change notification settings - Fork 0
/
023.go
50 lines (44 loc) · 921 Bytes
/
023.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package p023
/**
Merge k sorted linked lists and return it as one sorted list.
Analyze and describe its complexity.
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type ListNode struct {
Val int
Next *ListNode
}
func mergeKLists(lists []*ListNode) *ListNode {
if len(lists) <= 0 {
return nil
} else if len(lists) == 1 {
return lists[0]
} else if len(lists) == 2 {
return mergeTwoLists(lists[0], lists[1])
} else {
half := len(lists) / 2
return mergeTwoLists(mergeKLists(lists[:half]), mergeKLists(lists[half:]))
}
}
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
} else if l2 == nil {
return l1
}
head := &ListNode{0, l1}
l1 = head
for l1 != nil && l2 != nil {
for l1.Next != nil && l1.Next.Val <= l2.Val {
l1 = l1.Next
}
l1.Next, l2 = l2, l1.Next
}
return head.Next
}