Flatten multilevel list

Write codeMedium25 min

Your task

A multilevel doubly linked list is given as a nested structure: a list of nodes where each node is [val, child] and child is either null (encoded as None / empty), represented here as another nested list of the same shape, or [] for no child. Flatten it depth-first so a node's child sublist is spliced in right after that node and before the rest of the level. Return the flattened list of values in order.

Examples

flatten_multilevel(nodes: list) -> list[int]
Example 1
Inputflatten_multilevel([[1, []], [2, [[3, []], [4, []]]], [5, []]])
Output[1, 2, 3, 4, 5]