← 返回 bloomberg 的题目列表Flatten a Linked List with Down Pointers
类型:online_judge
Problem
You are given a linked list where each node contains:
val: integer value
next: pointer to the next node on the same level (nullable)
down: pointer to the head of a child sublist (nullable)
Flatten the multilevel list in-place into a single-level list with the following rules:
If a node x has a non-null down, insert the down list between x and x.next.
After flattening, all down pointers must be null.
The resulting order corresponds to a DFS traversal (expand down first, then continue with next).
Implement:
Node* flatten(Node* head)
Note: You may provide a clear recursive or iterative approach.
Example
Input
# Structure-based linked list input is omitted.
Output
# Flattened order follows DFS; down pointers all null.