Description
You have a multilevel doubly linked list where nodes may have a child pointer leading to a separate doubly linked list. Flatten all lists into a single level.
Examples
Input:
head with child pointersOutput:
flattened listExplanation:
DFS to flatten children between current and next.
Input:
[1,2,null,3,null,4,5]Output:
[1,3,4,5,2]Explanation:
The first node (1) has a child pointer to node 3. Flattening inserts the child subtree (3->4->5) between node 1 and its original next node 2. This demonstrates the basic flattening operation where a child list is spliced into the main list.
Input:
[10,20,30,null,40,null,null,50,60]Output:
[10,40,50,60,20,30]Explanation:
Node 10 has a child pointer to node 40, and node 40 has a child pointer to node 50. This shows nested children where it is necessary to recursively flatten multiple levels. Processing 10's child (40) first, then 40's child (50->60), creating a depth-first traversal through the multilevel structure.
Constraints
- •
Number of nodes ≤ 1000 - •
1 ≤ Node.val ≤ 10⁵