美文网首页
430. 扁平化多级双向链表

430. 扁平化多级双向链表

作者: 漫行者_ | 来源:发表于2021-09-24 00:57 被阅读0次

430. 扁平化多级双向链表

这题的关键还是全局变量的处理,重新温习了之前写的。
https://www.jianshu.com/p/81726e5284af

class Solution {
    Node fend;
    public Node flatten(Node head) {
        if(head == null) {
            return null;
        }
        Node p = head;
        while(p != null && p.child == null) {
            if(p.next == null) {
                fend = p;
            }
            p = p.next;
        }
        if(p == null) {
            return head;
        }
        Node pre = p.prev;
        Node next = p.next;
        Node child = p.child;
        p.child = null;
        Node flat = flatten(child);
        p.next = flat;
        flat.prev = p;
        fend.next = next;
        if(next != null) {
            next.prev = fend; 
        }
        while(fend.next != null) {
            fend = fend.next;
        }
        return head;   
    }
}

相关文章

网友评论

      本文标题:430. 扁平化多级双向链表

      本文链接:https://www.haomeiwen.com/subject/fepagltx.html