Description
Submission
/* // 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 nullptr; if(head->child != nullptr) { Node* next = head->next; head->next = head->child; head->child = nullptr; head->next->prev = head; Node* p = head->next; for(; p->next != nullptr; p = p->next); p->next = next; if(next != nullptr) next->prev = p; } head->next = flatten(head->next); if(head->next != nullptr) head->next->prev = head; return head; } };