多级链表
作者:
goldstine
,
2022-05-03 20:59:20
,
所有人可见
,
阅读 256
多级链表的扁平化处理
多级链表的扁平化
class Solution {
public Node flatten(Node head) {
dfs(head);
return head;
}
public Node dfs(Node head){
Node cur=head;
Node last=null;
while(cur!=null){
Node next=cur.next;
if(cur.child!=null){
Node childNode=dfs(cur.child);
cur.next=cur.child;
cur.next.prev=cur;
if(next!=null){
childNode.next=next;
childNode.next.prev=childNode;
}
cur.child=null;
last=childNode;
}else{
last=cur;
}
cur=next;
}
return last;
}
}
多级链表的复制
通过dfs进行多级链表的复制