forked from HuberTRoy/leetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeKSortedLists.py
58 lines (43 loc) · 1.14 KB
/
MergeKSortedLists.py
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
"""
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
合并 k 个有序链表。
用了最小堆:
把所有的链表节点入堆,然后出堆形成新的链表即可。
依然依靠了内置模块,待自己书写堆。
测试地址:
https://leetcode.com/problems/merge-k-sorted-lists/description/
"""
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
import heapq
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
a = []
heapq.heapify(a)
for i in lists:
while i:
heapq.heappush(a, i.val)
i = i.next
if not a:
return None
root = ListNode(heapq.heappop(a))
head = root
while a:
root.next = ListNode(heapq.heappop(a))
root = root.next
return head