141. Flatten a Multilevel Doubly Linked List
Medium · Linked List
You have a multilevel doubly linked list encoded as a nested array. Plain numbers are sibling nodes in the current level; a nested array after a number represents that node's child list, which may itself contain nested arrays (children of children). Your task is to flatten this entire structure into a single-level array, visiting nodes in depth-first order: process each node, then immediately process all its descendants before moving to the next sibling.
Input: a nested array representing the multilevel list (e.g. [1, 2, 3, [7, 8, [11, 12], 9, 10], 4, 5, 6]).
Output: a plain array of all values in depth-first order (e.g. [1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6]).
Examples
Example 1 Input: [1, 2, 3, [7, 8, [11, 12], 9, 10], 4, 5, 6] Output: [1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6] Explanation: Depth-first flattening of the nested structure
Example 2 Input: [1, 2, [3]] Output: [1, 2, 3] Explanation: Node 2 has a child list [3]
Constraints
- Standard input/output constraints apply