-
Notifications
You must be signed in to change notification settings - Fork 77
/
23.合并k个排序链表.go
60 lines (52 loc) · 1015 Bytes
/
23.合并k个排序链表.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
/*
* @lc app=leetcode.cn id=23 lang=golang
*
* [23] 合并K个排序链表
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
import (
"container/heap"
)
type srHeap []*ListNode
func (h *srHeap) Less(i, j int) bool {
return (*h)[i].Val < (*h)[j].Val
}
func (h *srHeap) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
func (h *srHeap) Len() int {
return len(*h)
}
func (h *srHeap) Pop() (v interface{}) {
*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
return
}
func (h *srHeap) Push(v interface{}) {
*h = append(*h, v.(*ListNode))
}
// MergeKLists 合并k个有序链表
func mergeKLists(lists []*ListNode) *ListNode {
heads := make(srHeap, 0)
for _, h := range lists {
if h != nil {
heads = append(heads, h)
}
}
heap.Init(&heads)
dummy := &ListNode{}
c := dummy
for len(heads) > 0 {
c.Next = heap.Pop(&heads).(*ListNode)
c = c.Next
if c.Next != nil {
heap.Push(&heads, c.Next)
}
}
return dummy.Next
}