递归+模拟
/*
// Definition for a Node.
class Node {
public:
int val;
Node* prev;
Node* next;
Node* child;
};
*/
class Solution {
public:
Node* flatten(Node* head) {
if(head == nullptr) return head;
if(head ->child == nullptr){
head ->next = flatten(head ->next);
}else{
Node* temp = flatten(head ->child);
head ->child = nullptr;
Node* trans = temp;
while(trans ->next != nullptr) trans = trans ->next;
if(head ->next != nullptr){
head ->next ->prev = trans;
trans ->next = head ->next;
head ->next ->next = flatten(head ->next ->next);
}
head ->next = temp;
temp ->prev = head;
}
return head;
}
};