Given k sorted linked lists each of size n, merge them and print the sorted 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 dicussed 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.
The process must start with creating a MinHeap and inserting the first element of all the k linked list. Remove the root element of Minheap and insert it in the output list and insert the next element from the linked list of removed element. To get the result the step must continue until there is no element in the MinHeap left.
0 1 2 3 4 5 6 7 8 9 10 11
- Time Complexity: O( n * k * log k).
Insertion and deletion in a Min Heap requires log k time. So the Overall time complexity is O( n * k * log k)
- Auxiliary Space: O(k).
k is the space required to store the priority queue.
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Sorted merge of two sorted doubly circular linked lists
- Merge K sorted linked lists | Set 1
- Merge two sorted linked lists
- Merge two unsorted linked lists to get a sorted list
- Merge two sorted linked lists such that merged list is in reverse order
- Merge two sorted arrays in constant space using Min Heap
- Merge K sorted Doubly Linked List in Sorted Order
- Merge two sorted lists (in-place)
- Merge Sort for Linked Lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Difference of two Linked Lists using Merge sort
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- In-place Merge two linked lists without changing links of first list
- Merge two sorted linked list without duplicates
- Intersection of two Sorted Linked Lists
- Create a linked list from two linked lists by choosing max element at each position
- Insert value in sorted way in a sorted doubly linked list
- Merge k sorted arrays | Set 1
- Merge two sorted arrays