-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeklists.py
55 lines (41 loc) · 1.12 KB
/
mergeklists.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
"""
Task:
Merge k sorted linked lists and return it as one sorted list.
Examples:
>>> l1 = nums_2_list([10, 20, 30])
>>> l2 = nums_2_list([15, 25, 28])
>>> list_2_nums(merge_k_lists([l1, l2]))
[10, 15, 20, 25, 28, 30]
"""
import heapq
from typing import List, Iterable
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
def nums_2_list(nums: Iterable[int]) -> ListNode:
head = ListNode(None)
cur = head
for num in nums:
cur.next = ListNode(num)
cur = cur.next
return head.next
def list_2_nums(head: ListNode) -> List[int]:
return list(NodeIter(head))
class NodeIter(object):
def __init__(self, node):
self.cur = node
def __iter__(self):
return self
def __next__(self):
if self.cur is None:
raise StopIteration
else:
val = self.cur.val
self.cur = self.cur.next
return val
def merge_k_lists(lists: List[ListNode]) -> ListNode:
lists = [NodeIter(head) for head in lists]
merged = heapq.merge(*lists)
head = nums_2_list(merged)
return head