This problem can be solved using a min-heap of size k.
The min-heap will always store the first unmerged node from each of the k sorted linked lists, and the node with the smallest value sits at the root of the heap. We build the merged list we will return at the end by repeatedly popping the root from the heap. Each time we pop from the heap, we will also push the next element from the linked list where the popped element came from onto the heap.
When the heap is empty, all the elements from the linked lists have been merged, and we can return the merged list.
Step 1: Initialize the heap
The first step in the algorithm is to initialize the heap with the first element from each of the k linked lists. We iterate over each of the k linked lists, and for each linked list, we push a tuple containing the value of its head node, its index (in the input list), and the node itself onto the heap.
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
initialize heap
0 / 3
1x
Step 2: Build the merged list
With the heap initialized, we can now build the merged linked list. We start by initializing a dummy node that acts as the head of the merged list, and a pointer curr that points to the last node in the merged list (initially the dummy node).
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
346235-16-1,23,02,1
add -1 to heap
0 / 1
1x
Now, we can repeatedly pop the root of the heap, which gives us a reference to the node with the smallest value among the first unmerged elements from the k linked lists. We append this node to the merged list and move the pointer curr to the node we just appended.
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
346235-16-1,23,02,10curr
initialize dummy node
0 / 1
1x
Last, to ensure that our heap always contains the first unmerged element from each of the k linked lists, we push the next node from the linked list where the popped node came from onto the heap, if it exists. This prepares the heap for the next iteration.
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
346235-162,13,00-1curr
add -1 to result
0 / 1
1x
When the heap is empty, all the elements from the linked lists have been merged, and we can return head of the the merged list by returning dummy.next.
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
346235-160-12334566curr
0 / 1
1x
Solution
from heapq import heappush, heappop
defmergeKLists(lists):
ifnot lists:
returnNone
heap =[]
for i, node inenumerate(lists):
if node:
heappush(heap,(node.val, i, node))
dummy = ListNode(0)
curr = dummy
while heap:
val, idx, node = heappop(heap)
curr.next= node
curr = curr.next
if node.next:
heappush(heap,(node.next.val, idx, node.next))
return dummy.next
346235-16
merge k sorted lists
0 / 19
1x
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n * log k) where n is the total number of nodes across all input linked lists and k is the number of linked lists. We iterate over all the nodes in the linked lists. At each iteration, we push and pop elements from the heap, which takes O(log k) time.
Space Complexity: O(k) where k is the number of linked lists. We use a min-heap of size k to store the first unmerged element from each of the k linked lists.
Your account is free and you can post anonymously if you choose.