-
Notifications
You must be signed in to change notification settings - Fork 116
/
merge_sorted_list.go
63 lines (50 loc) · 1.03 KB
/
merge_sorted_list.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
51
52
53
54
55
56
57
58
59
60
61
62
63
package heap
import (
"container/heap"
"github.com/spring1843/go-dsa/linkedlist"
)
type (
priorityQueue []*linkedlist.Node
)
// MergeSortedLists solves the problem in O(n*Log k) time and O(k) space.
func MergeSortedLists(lists []*linkedlist.Node) *linkedlist.Node {
pq := new(priorityQueue)
for _, item := range lists {
if item == nil {
continue
}
heap.Push(pq, item)
}
var head, cur *linkedlist.Node
for pq.Len() > 0 {
node := heap.Pop(pq).(*linkedlist.Node)
if cur == nil {
cur = node
head = cur
continue
}
cur.Next = node
cur = cur.Next
if node.Next != nil {
heap.Push(pq, node.Next)
}
}
return head
}
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].Val < pq[j].Val
}
func (pq *priorityQueue) Pop() any {
old := *pq
n := len(old)
x := old[n-1]
*pq = old[0 : n-1]
return x
}
func (pq *priorityQueue) Push(x any) {
*pq = append(*pq, x.(*linkedlist.Node))
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}