This is a tree-to-list conversion in disguise. The multilevel structure is a tree where each node has a "next sibling" (the next pointer) and a "first child" (the child pointer). Flattening it means splicing every child chain into the main list, right after its parent node.
The complication is nested children. A child list can have children of its own, forming arbitrarily deep nesting, so the solution has to keep flattening until every child pointer is gone.
When we reach a node with a child, the child chain has to be inserted between that node and its next node, and the child pointer cleared afterward. If the child chain has children of its own, those are flattened the same way once they become part of the main list.
Number of nodes <= 1000 → The list is small, but the splice-in-place approach below runs in O(n) with O(1) extra space, so there is no reason to settle for anything slower.prev, next, child) must stay consistent. Two recurring bugs are forgetting to null out the child pointer after splicing and forgetting to fix the prev pointer of the node that follows the spliced chain.Flattening visits nodes in pre-order: a node, then its child subtree, then its next sibling. Linking nodes in that visit order produces the required single-level list.
A stack makes the order explicit. When we process a node, we push its next first and its child second. Because the stack is LIFO, the child comes off the stack before the next sibling, so the child chain gets linked in first. Pushing the next sibling before descending into the child is what lets us return to the sibling after the entire child subtree has been flattened.
prev node to simplify pointer management.prev (set prev.next = curr, curr.prev = prev).next, push next onto the stack.child, push child onto the stack, then set the node's child to null.prev = curr.head.prev = null.The stack uses O(n) extra space. The next approach removes it by reusing the pointers already in the list: it walks to the tail of each child chain and stitches that tail directly onto the parent's next node.
We can flatten the list in place during a single forward walk, with no stack. When the current node has a child, we splice the child chain in: find the tail of the child chain, point the current node at the child, point the child back at the current node, then connect the tail to whatever the current node used to point at. After clearing the child pointer, we continue walking from the current node, which now leads into the just-spliced chain.
Walking forward into the spliced chain instead of skipping past it is what handles nested children. A child two levels deep becomes part of the main list as soon as its parent is spliced in, so the same loop reaches it on a later iteration with no special handling.
Finding the tail of a child chain takes O(k) for a chain of length k, so a naive count suggests this could be O(n^2). It is not. Once a child chain is spliced into the main list, its nodes are no longer reachable through any child pointer, so they are never part of another tail search. Each node is scanned by at most one tail search over the whole run. The tail searches therefore cost O(n) in total, and the outer walk visits each node once, giving O(n) overall.
curr at the head.curr is not null:curr has a child: a. Save curr.next as next.
b. Walk to the tail of the child chain.
c. Connect curr.next = curr.child and curr.child.prev = curr.
d. Connect tail.next = next and, if next is not null, next.prev = tail.
e. Set curr.child = null.
curr = curr.next.curr, next, tail). No stack or recursion needed.