Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.
题目: 430. Flatten a Multilevel Doubly Linked List
)
分析:
当遇到有child的Node,先用*next
存cur->next; 把cur->next指针指向child, child->pre指向cur; 接着以child为起点找到该曾最后一个Node,让它指向*next
的节点。 接着以原来的child为cur Node重新开始一轮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class Node { public: int val; Node *prev; Node *next; Node *child;
Node() {}
Node(int _val, Node *_prev, Node *_next, Node *_child) { val = _val; prev = _prev; next = _next; child = _child; } };
Node *flatten(Node *head) { Node *cur = head; while (cur) { if (cur->child) { Node *next = cur->next; cur->next = cur->child; cur->child = nullptr; cur->next->prev = cur;
Node *p = cur->next; while (p->next) p = p->next; p->next = next; if (next) next->prev = p; } cur = cur->next; }
return head; }
|
时间复杂度: $O(n)$