-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_merge_k_sorted_LL.go
92 lines (69 loc) · 1.82 KB
/
9_merge_k_sorted_LL.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
import (
"container/heap"
)
type Node struct {
Val int
Pointer *ListNode
}
// A NodeHeap is a min-heap of Nodes.
type NodeHeap []Node
func (h NodeHeap) Len() int { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].Val < h[j].Val }
func (h NodeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *NodeHeap) Push(x interface{}) {
*h = append(*h, x.(Node))
}
func (h *NodeHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func mergeKLists(pointers []*ListNode) *ListNode {
var final_merged_LL *ListNode = nil
h := &NodeHeap{}
heap.Init(h)
for _, pointer := range pointers {
if pointer != nil {
nodeVal := Node { pointer.Val , pointer }
heap.Push(h, nodeVal)
}
}
var curr *ListNode = nil
for h.Len() > 0 {
topVal := heap.Pop(h).(Node)
if final_merged_LL == nil {
final_merged_LL = new(ListNode)
final_merged_LL.Val = topVal.Val
// final_merged_LL.Next = nil
curr = final_merged_LL
} else {
curr.Next = new(ListNode)
curr = curr.Next
curr.Val = topVal.Val
}
topVal.Pointer = topVal.Pointer.Next
if topVal.Pointer != nil {
nodeVal := Node { topVal.Pointer.Val , topVal.Pointer }
heap.Push(h,nodeVal)
}
}
return final_merged_LL
}
/*
k sorted linked lists
l1 , l2 , l3, l4, lk
l1 + l2 + l3 + l4 .... + lk
1e4 * 500 = 5e6 max total len
here we merge k sorted linked lists
we make a min heap on the basis of the list values
and eliminate the elements one by one from the heap
*/