You are given a linked list where each node contains two pointers:
Each sub-linked list is sorted in ascending order. Flatten the Link List so all the nodes appear in a single level while maintaining the sorted order.
Note: The flattened linked list should be printed using the bottom pointer instead of the next pointer. Refer to the printList() function in the driver code for better understanding.
Example 1
Input: 5 -> 10 -> 19 -> 28 | | | | 7 20 22 35 | | | 8 50 40 | | 30 45
Output: 5->7->8->10->19->20->22->28->30->35->40->45->50
Explanation: The resultant linked lists has every node in a single level.(Note: | represents the bottom pointer.)
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
2 1 3 2 1 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
4 4 3 2 5 10 19 28 7 20 22 35 8 40 50 30 45
5 7 8 10 19 20 22 28 30 35 40 45 50
2 1 1 2 3
1 2 3