πDay 3. Merge K sorted linked lists π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given an array arr[] of n sorted linked lists of different sizes, merge them into a single sorted linked list and return the head of the merged list.
π Example Walkthrough:
Example 1:
Input:
arr[] = [1 -> 2 -> 3, 4 -> 5, 5 -> 6, 7 -> 8]
Output:
1 -> 2 -> 3 -> 4 -> 5 -> 5 -> 6 -> 7 -> 8
Explanation:
The arr[]
has 4 sorted linked lists of sizes 3, 2, 2, 2.
1st list: 1 -> 2 -> 3
2nd list: 4 -> 5
3rd list: 5 -> 6
4th list: 7 -> 8
After merging, the final sorted linked list is:
1 -> 2 -> 3 -> 4 -> 5 -> 5 -> 6 -> 7 -> 8
Example 2:
Input:
arr[] = [1 -> 3, 8, 4 -> 5 -> 6]
Output:
1 -> 3 -> 4 -> 5 -> 6 -> 8
Explanation:
The arr[]
has 3 sorted linked lists of sizes 2, 1, 3.
1st list: 1 -> 3
2nd list: 8
3rd list: 4 -> 5 -> 6
After merging, the final sorted linked list is:
1 -> 3 -> 4 -> 5 -> 6 -> 8
Constraints:
$(1 \leq \text{Total number of nodes} \leq 10^5)$
$(1 \leq \text{node->data} \leq 10^3)$
π― My Approach:
Min-Heap (Priority Queue)
Use a Min-Heap (Priority Queue) to store the head of each linked list.
Extract the smallest element from the heap and add it to the merged list.
Move the pointer of the extracted nodeβs list to the next node and push it back into the heap.
Repeat until all lists are merged.
Algorithm Steps:
Push the first node of each linked list into the min-heap.
While the heap is not empty:
Extract the smallest node from the heap.
Add it to the result linked list.
If the extracted node has a next node, push it into the heap.
Return the merged list starting from the dummy nodeβs next pointer.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N log K), Each node is pushed & popped from the heap once β O(N log K) (heap operations).
Expected Auxiliary Space Complexity: O(K), Heap stores at most K elements at a time β O(K) extra space.
π Solution Code
Code (C++)
class Solution {
public:
Node* mergeKLists(vector<Node*>& l) {
auto c=[](Node* a,Node* b){return a->data>b->data;};
priority_queue<Node*,vector<Node*>,decltype(c)> pq(c);
for(auto x:l) if(x) pq.push(x);
Node d(0),*t=&d;
while(!pq.empty()){
t->next=pq.top(); t=pq.top(); pq.pop();
if(t->next) pq.push(t->next);
}
return d.next;
}
};
Code (Java)
class Solution {
public Node mergeKLists(List<Node> lists) {
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a.data));
for (Node head : lists) if (head != null) pq.add(head);
Node dummy = new Node(0), tail = dummy;
while (!pq.isEmpty()) {
tail.next = pq.poll();
tail = tail.next;
if (tail.next != null) pq.add(tail.next);
}
return dummy.next;
}
}
Code (Python)
class Solution:
def mergeKLists(self, lists):
heap = [(node.data, i, node) for i, node in enumerate(lists) if node]
heapq.heapify(heap)
dummy = tail = Node(0)
while heap:
_, i, node = heapq.heappop(heap)
tail.next, tail = node, node
if node.next:
heapq.heappush(heap, (node.next.data, i, node.next))
return dummy.next
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated