Input: Given linked list is:
Output:
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 12 → 20 → null.
Explanation:
The returned linked list should be in a sorted order. All the elements in this returned linked list are connected by 'child' pointers and 'next' pointers point to null.
The first line of the input contains a single integer ‘n’ which represents the number of head nodes in the linked list.
The next '2n' lines represent 'n' linked lists connected by next pointer at the head. Description of each of these linked lists is as follows:
The first line contains a single integer 'k', number of nodes in the current linked list.
The next line contains 'k' spaced integers, representing elements of the linked list.
Return the head node of the final linked list.
You don’t have to print anything, it has already been taken care of. Just implement the given function. The flattened list will be printed using the child pointer.
The idea is to use an extra array to first store all the elements of the linked list and then sort the array and finally put those elements back into the linked list and return.
The idea is to use the given situation that each sublist is sorted and there are a total of N nodes that means there are N sorted sublists. So the idea is to sort the whole thing.
The idea is to merge the linked list in place and merge it recursively with the current flattened list. Basically, we are traversing through the linked list and each time, we are merging the two-child lists into one. We will repeat this process until we are left with only one final linked list containing all the nodes. In short, we repeatedly merge the current list with the already flattened list.