题目
https://leetcode-cn.com/explore/learn/card/linked-list/197/conclusion/764/
代码
/*
// Definition for a Node.
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;
}
};
*/
class Solution {
public:
Node* flatten(Node* head) {
if(head==nullptr)
return nullptr;
Node*ptr=head;
while(ptr!=nullptr)
{
//当前结点有孩子结点
if(ptr->child!=nullptr)
{
auto child=ptr->child;
auto ptrNext=ptr->next;
child->prev=ptr;
ptr->next=child;
ptr->child=nullptr;
while(child->next!=nullptr)
child=child->next;
//当前结点不是尾结点
if(ptrNext!=nullptr)
{
ptrNext->prev=child;
child->next=ptrNext;
}
}
ptr=ptr->next;
}
return head;
}
};