← 返回 bloomberg 的题目列表Flatten a Multilevel Doubly Linked List
类型:online_judge
Given the head of a multilevel doubly linked list where each node has val, prev, next, and child pointers. The child pointer may point to another doubly linked list, which can have its own children.
Flatten the list in-place into a single-level doubly linked list using depth-first order: visit the node, then its child list (fully flattened), then continue with the original next.
After flattening:
All child pointers must be null.
prev/next pointers must form a valid doubly linked list.
Return the head of the flattened list.
Constraints: 1 <= N <= 1000 nodes.
Conceptual example: 1-2-3-4 with 2.child = 7-8 and 8.child = 11-12 becomes 1-2-7-8-11-12-3-4.
Example
Input
conceptual
1-2-3-4
2.child=7-8
8.child=11-12
Output
1-2-7-8-11-12-3-4