


You are given a multi-level linked list of 'N' nodes, each node has a next and child pointer which may or may not point to a separate node. Flatten the multi-level linked list into a singly linked list. You need to return the head of the updated linked list.
Example:

Flatten :
All the different rows are merged into a single row.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains the elements of the multi-level linked-list in the level order form. The line consists of values of nodes separated by a single space. In case a node(next or child pointer) is null, we take -1 in its place.
For example, the input for the multi-level linked-list depicted in the below image would be :

10
5 4
12 -1 20 -1
7 -1 13 2
11 17 -1 16 -1 -1
-1 -1 -1 -1 -1 -1
Explanation :
Level 0 :
The head node of the multi-level linked-list is 10
Level 1 :
Next pointer of 10 = 5
Child pointer of 10 = 4
Level 2 :
Next pointer of 5 = 12
Child pointer of 5 = null (-1)
Next pointer of 4 = 20
Child pointer of 4 = null(-1)
Level 3 :
Next pointer of 12= 7
Child pointer of 12 = null(-1)
Next pointer of 20 = 13
Child pointer of 20 = 2
Level 4 :
Next pointer of 7 = 11
Child pointer of 7 = 17
Next pointer of 13 = null(-1)
Child pointer of 13 = 16
Next pointer of 2 = null(-1)
Child pointer of 2 = null(-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level(next and child pointers). The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given multi-level linked list.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted multi-level linked list, the input will be given as:
10 5 4 12 -1 20 -1 7 -1 13 2 11 17 -1 16 -1 -1 -1 -1 -1 -1 -1 -1
Output format :
For each test case, print the flattened linked-list of the multi-level linked-list in a single line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
0 <= N <= 10^4
1 <= data <= 10^9
Time Limit : 1 sec
1
1 -1 2 -1 3 -1 4 -1 -1
1 2 3 4
The given multi-level linked-list will be represented as shown below

1
10 5 4 12 -1 20 -1 7 -1 13 2 11 17 -1 16 -1 -1 -1 -1 6 9 -1 3 -1 -1 8 19 -1 -1 -1 -1 15 -1 -1 -1
10 5 12 7 11 4 20 13 17 6 2 16 9 8 3 19 15
Traverse the list and make new connections(next) every time a new level(row) is reached.
The naive solution is to maintain a pointer 'TAIL’ which initially points to the end of level 0(level that contains head). And the problem now is to make necessary connections so that all nodes are traversed through the 'TAIL' via the next parameter only. Take another pointer 'CURR' and traverse the list till the end of the list(null). Make a new connection i.e 'TAIL'->next = 'CURR'->child (append current row head to previous row 'TAIL'). Also, maintain a pointer ‘TEMP' which maintains the tail of the flatten linked list. For better understanding given below is the algorithm for the approach.
Algorithm:
O(N), where ‘N’ is the number of nodes in the multi-level linked list.
As every node of the multi-level linked list is visited only at max twice.
O(1).
Only constant extra space is required.