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 Output: 0->1->2->3->4->5->6->7->8->9->10->11
Approach: An efficient solution for the problem has been dicussed in Method 3 of this post. Here another solution has been provided which the uses the MIN HEAP data structure. This solution is based on the min heap approach used to solve the problem ‘merge k sorted arrays’ which is discussed here.
0 1 2 3 4 5 6 7 8 9 10 11
Time Complexity: O(nk Logk)
Auxiliary Space: 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Sorted merge of two sorted doubly circular linked lists
- 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 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