Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.
Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10->11 Merged lists in a sorted order where every element is greater than the previous element. Input: k = 3, n = 3 list1 = 1->3->7->NULL list2 = 2->4->8->NULL list3 = 9->10->11->NULL Output: 1->2->3->4->7->8->9->10->11 Merged lists in a sorted order where every element is greater than the previous element.
An efficient solution for the problem has been discussed in Method 3 of this post.
Approach: This solution is based on the MIN HEAP approach used to solve the problem ‘merge k sorted arrays’ which is discussed here.
MinHeap: A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. Mapping the elements of a heap into an array is trivial: if a node is stored at index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2.
- Create a min-heap and insert the first element of all the ‘k’ linked lists.
- As long as the min-heap is not empty, perform the following steps:
- Remove the top element of the min-heap (which is the current minimum among all the elements in the min-heap) and add it to the result list.
- If there exists an element (in the same linked list) next to the element popped out in previous step, insert it into the min-heap.
- Return the head node address of the merged list.
Below is the implementation of the above approach:
0 1 2 3 4 5 6 7 8 9 10 11
- Time Complexity: O(N * k * log k), where, ‘N’ is the total number of elements among all the linked lists and ‘k’ is the total number of lists.
Insertion and deletion in a min-heap requires log k time. So the overall time complexity is O(N * log k).
- Auxiliary Space: O(k).
The priority queue will have atmost ‘k’ number of elements at any point of time, hence the additional space required for our algorithm is O(k).
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Merge two sorted arrays in constant space using Min Heap
- Sorted merge of two sorted doubly circular linked lists
- Heap Sort for decreasing order using min heap
- Convert min Heap to max Heap
- Merge K sorted linked lists | Set 1
- Merge two sorted linked lists
- Merge two sorted linked lists such that merged list is in reverse order
- Merge two unsorted linked lists to get a sorted list
- Merge K sorted Doubly Linked List in Sorted Order
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Merge two sorted lists (in-place)
- Difference of two Linked Lists using Merge sort
- Maximise the number of toys that can be purchased with amount K using min Heap
- Merge Sort for Linked Lists
- In-place Merge two linked lists without changing links of first list
- Merge odd and even positioned nodes of two Linked Lists alternately
- k largest(or smallest) elements in an array | added Min Heap method
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap