-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-k-sorted-lists.py
35 lines (31 loc) · 1.2 KB
/
merge-k-sorted-lists.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
# time O(kN) & space O(N) for the returned linked list,
# where k is the number of list and N is total number of nodes
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if not lists or not any(lists):
return None
def find_indices_with_min_val(lists):
indices = []
min_val = sys.maxsize
for index, node in enumerate(lists):
if node.val < min_val:
min_val = node.val
indices = [index]
elif node.val == min_val:
indices.append(index)
return min_val, indices
prev_head = curr = ListNode()
while any(lists):
lists = [l for l in lists if l]
min_val, indices = find_indices_with_min_val(lists)
for index in indices:
curr.next = ListNode(val=min_val)
curr = curr.next
lists[index] = lists[index].next
curr.next = None
return prev_head.next