A doubly linked list where nodes may have a child pointer to another doubly linked list. Flatten it so all nodes appear in a single-level doubly linked list (depth-first).
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation: Depth-first: follow child before continuing next.
Output: [1,3,2]
Iterate with a stack. When a child is encountered, push the next node onto a stack, then follow the child. When current.next is null, pop from stack if available.
- Iterative: when node.child != null, insert child list between node and node.next
- Find tail of child list, connect to node.next
Complexity Analysis:
Time complexity: O(n)
Space complexity: O(1)