You are given the head of a multilevel doubly linked list. In this list, nodes may have a child pointer to a separate doubly linked list. These child lists may themselves have children, forming multiple levels.
Flatten the list so that all nodes appear in a single-level doubly linked list. The nodes should appear in depth-first order (DFS) — when a node has a child, the child sublist is inserted between the node and its next sibling.
The input is encoded as a flat array using the LeetCode convention: non-null values are node values in order; a sequence of null values separates levels. The number of nulls before a child group indicates the 0-indexed position (minus 1) within the parent group where the child attaches.
Return the flattened list as an array of values in DFS order.
Example 1:
Input: head = [1,2,3,4,5,6,null,null,null,7,8,null,null,11,12]
Output: [1,2,3,7,8,11,12,4,5,6]
Explanation:
Example 2:
Input: head = [1,2,null,3]
Output: [1,3,2]
Explanation:
Example 3:
Input: head = []
Output: []
Explanation: Empty list returns empty.
[0, 1000]1 <= Node.val <= 10⁶1000