-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh23.py
29 lines (24 loc) · 1.03 KB
/
h23.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
output = ListNode() # Dummy node
current = output
# heap will prioritize checking node.val -> i
# Need i to avoid it relying on Node <= Node then causing type errors
# i is unique
# heap will have at most k values at any point so minimal cost for operations
newLists = [(node.val, i, node) for i, node in enumerate(lists) if node]
heapq.heapify(newLists)
i = len(newLists)
while newLists :
poppedNode = heapq.heappop(newLists)[2]
if poppedNode.next : # if another node is found after
i += 1
heapq.heappush(newLists, (poppedNode.next.val, i, poppedNode.next))
current.next = poppedNode
current = current.next
return output.next