430. Flatten a Multilevel Doubly Linked List

摘要:
=null){if(cur.child==null)}cur=cur.next;continue;}NodechildTail=cur.child;while(childTail.next!=null){childTail=childTail.nnext;}if(cur.next!=null){childTail.innext=next;next.prev=childTail;returndfs(next);}returnchildTail;}}

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:

430. Flatten a Multilevel Doubly Linked List第1张

We should return the following flattened doubly linked list:

430. Flatten a Multilevel Doubly Linked List第2张

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;
        }
    }
}

免责声明:文章转载自《430. Flatten a Multilevel Doubly Linked List》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【js html】对于<img>图片的引用填充,src可以给什么值?707. Design Linked List下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Leetcode 2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and...

[Leetcode] Swap Nodes in Pairs

Swap Nodes in Pairs 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/swap-nodes-in-pairs/description/ Description Given a linked list, swap every two adjacent nodes and return its...

复杂链表的复制

题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) 1、复制每个节点,如:复制节点A得到A1,将A1插入节点A后面 2、遍历链表,A1->random = A->r...

动态内存分配

动态内存分配及练习malloc与free使用时的注意事项:1)配对使用,有一个malloc,就应该有一个free2)尽量在同一层上使用,不要出现malloc在函数中,而free在函数外。最好在同一调用层上使用这两个函数3)malloc分配的内存一定要初始化free后的指针不允许再使用链表的基本操作链表的构成:链表通过在每个结构体变量中设置指向下一个变量的指针...

最小生成树K氏法

K氏法 K氏法也叫Kruskal算法,是将各边按照权值从小到大排列,接着从权值最低的边开始建立最小成本生成树,如果加入的边造成了贿赂,则舍弃不用,直到加入n-1个边为止。 示例 把所有边的成本列出,按照从小到大排列。 起始 终止 成本 B C 3 B D 5 A B 6 C D 7 B F 8 D E 9 A E 10...

117.Populating Next Right Pointers in Each Node II

Given a binary tree struct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode *next;} Populate each next pointer to point to its next right node. If there is no next...

最新文章