Given a linked list where every node represents a linked list and contains two pointers of its type:
- Pointer to next node in the main list (we call it ‘right’ pointer in the below code)
- Pointer to a linked list where this node is head (we call it ‘down’ pointer in the below code).
All linked lists are sorted. See the following example
Input: 5 -> 10 -> 19 -> 28 | | | | V V V V 7 20 22 35 | | | V V V 8 50 40 | | V V 30 45 Output: 5->7->8->10->19->20->22->28->30->35->40->45->50 Input: 5 -> 10 -> 19 -> 28 | | V V 7 22 | | V V 8 50 | V 30 Output: 5->7->8->10->19->20->22->30->50
Approach: The idea is to observe that from each top node there are N nodes that are connected in a downward direction but observe that all the downward nodes are in sorted order. So the task is to sort this entire thing in increasing order(or decreasing order).
- Push the head of all the linked lists in the downward list in the priority queue.
- Pop the smallest node from the priority queue.
- Check the location of the node so that the next node pointed by the current node can be pushed into the priority queue.
- Again pop out the smallest element and insert the next node pointed by the current node till the heap becomes empty.
- Keep on adding the data of nodes in a new linked list that are pooped out to the new list.
- Print the linked list formed above.
Below is the implementation of the above approach:
5 7 8 10 19 20 20 22 30 35 40 45 50
Time Complexity: O(k * log k) + O((N-k) * log k) = O(N * log k), where ‘k‘ is the number of nodes in the topmost horizontal linked list and ‘N‘ is the total number of nodes among all the linked lists. ‘log k‘ time is taken for the min-heapify procedure.
Auxiliary Space: O(k) for the min-heap, where ‘k‘ is the number of nodes in the topmost horizontal linked list. The min-heap will have at the most ‘k‘ number of nodes at any time.
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.
- Flattening a Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Doubly linked linked list from 2D Matrix
- Sublist Search (Search a linked list in another list)
- Length of longest palindrome list in a linked list using O(1) extra space
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Clone a linked list with next and random pointer | Set 1
- Alternating split of a given Singly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.