430. Flatten a Multilevel Doubly Linked List - Medium

You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

 

Example:

Input:
 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

Output:
1-2-3-7-8-11-12-9-10-4-5-6-NULL

 

Explanation for the above example:

Given the following multilevel doubly linked list:

 

We should return the following flattened doubly linked list:

 

M1: straight forward

用一个pointer遍历linked list,遇到有child的节点,就把整个child linked list加入linked list,再继续遍历,直到走到null为止

time: O(n)  -- n: total # of nodes, space: O(1)

/*
// Definition for a Node.
class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node child;

    public Node() {}

    public 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 == null) {
            return head;
        }
        Node cur = head;
        while(cur != null) {
            if(cur.child == null) {
                cur = cur.next;
                continue;
            }
            Node childTail = cur.child;
            while(childTail.next != null) {
                childTail = childTail.next;
            }
            
            if(cur.next != null) {
                cur.next.prev = childTail;
            }
            childTail.next = cur.next;
            
            cur.next = cur.child;
            cur.child.prev = cur;
            cur.child = null;
            cur = cur.next;
        }
        return head;
    }
}

 

M2: recursion, dfs

1. head = null,或者只有head一个节点,没next/child,return;

2. 没child, next;

3. 有child,没next,flatten即可;

4. 有child,有next,flatten完,把cur.next和child的tail连接起来

time: O(n)  -- n: total # of nodes, space: O(k)  -- k: total # of child

/*
// Definition for a Node.
class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node child;

    public Node() {}

    public Node(int _val,Node _prev,Node _next,Node _child) {
        val = _val;
        prev = _prev;
        next = _next;
        child = _child;
    }
};
*/
class Solution {
    public Node flatten(Node head) {
        dfs(head);
        return head;
    }
    
    private Node dfs(Node head) {
        if(head == null) {
            return head;
        } else if(head.child == null) {
            if(head.next == null) {
                return head;
            }
            return dfs(head.next);
        } else {
            Node child = head.child;
            head.child = null;
            Node next = head.next;
        
            Node childTail = dfs(child);
            head.next = child;
            child.prev = head;
        
            if(next != null) {
                childTail.next = next;
                next.prev = childTail;
                return dfs(next);
            }
        
            return childTail;
        }
    }
}

 

转载于:https://www.cnblogs.com/fatttcat/p/10183095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值