forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_078.py
66 lines (49 loc) · 1.48 KB
/
problem_078.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
58
59
60
61
62
63
64
65
import sys
from heapq import heappush, heappop
class Node:
def __init__(self, x):
self.val = x
self.next = None
def __str__(self):
string = "["
node = self
while node:
string += "{} ->".format(node.val)
node = node.next
string += "None]"
return string
def get_nodes(values):
next_node = None
for value in values[::-1]:
node = Node(value)
node.next = next_node
next_node = node
return next_node
def get_list(head):
node = head
nodes = list()
while node:
nodes.append(node.val)
node = node.next
return nodes
def merge_lists(all_lists):
merged_head = Node(None)
merged_tail = merged_head
candidates = list()
counter = 0
for llist in all_lists:
heappush(candidates, (llist.val, counter, llist))
counter += 1
while candidates:
_, _, new_node = heappop(candidates)
if new_node.next:
heappush(candidates, (new_node.next.val, counter, new_node.next))
counter += 1
merged_tail.next = new_node
merged_tail = new_node
return merged_head.next
assert get_list(merge_lists([get_nodes([1, 4, 6]),
get_nodes([1, 3, 7])])) == [1, 1, 3, 4, 6, 7]
assert get_list(merge_lists([get_nodes([1, 4, 6]),
get_nodes([2, 3, 9]),
get_nodes([1, 3, 7])])) == [1, 1, 2, 3, 3, 4, 6, 7, 9]