Leetcode430. 扁平化多级双向链表
/*
// Definition for a Node.
class Node {
public:
int val;
Node* prev;
Node* next;
Node* child;
};
*/
class Solution {
public:
Node* flatten(Node* head) {
auto res = dfs(head);
return res[0];
}
vector<Node*> dfs(Node* head) {
if (!head) return {NULL, NULL};
auto cur = head, tail = head;
while (cur) {
tail = cur;
if (cur->child) {
auto t = dfs(cur->child);
cur->child = NULL;
if (cur->next) cur->next->prev = t[1];
t[1]->next = cur->next;
cur->next = t[0];
t[0]->prev = cur;
}
else cur = cur->next;
}
return {head, tail};
}
};
7868



被折叠的 条评论
为什么被折叠?



