Flatten The Multi-Level Linked List

Posted: 30 Oct, 2020
Difficulty: Moderate


Try Problem

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.


Sample Multi-Level

Flatten :

All the different rows are merged into a single row.
Input Format:
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 :

Sample Multi-level List

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.
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
Approach 1

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.



  • If the 'HEAD' is null, return 'HEAD'.
  • Initialize a 'TAIL' node with the 'HEAD' and iterate till the end of the first row so that the 'TAIL' node will store the end node of each row processed.
  • Initialize two nodes 'CURR' = 'HEAD' and 'TEMP' = null, where 'CURR' is used to traverse the whole list and 'TEMP' is used to update the 'TAIL'.
  • While 'CURR' is not null,
    • If the 'CURR' node has a child node,
      • then we will append this child to the end of the list. Now the next row will be attached to the end of the previous row.
      • Using the 'TEMP' node iterates to the end of this row until the next node is null.
      • Update the 'TAIL' node with the 'TEMP' i.e the new tail of the flatten list.
    • Shift the 'CURR' to its next node i.e 'CURR' = 'CURR'.next.
Try Problem