原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/

题目:

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:

题解:

如果当前点cur 没有child, 直接跳到cur.next 进行下次计算.

如果cur 有child, 目标是把cur.child这个level提到cur这个level上. 至于cur.child 这个level上有没有点有child 先不管.

做法就是cur.child 一直只按next找到tail, 然后这一节插在cur 和 cur.next之间, cur再跳到更新的cur.next上.

Time Complexity: O(n). n是所有点的个数, 每个点只走过constant次数.

Space: O(1).

AC Java:

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

类似Flatten Binary Tree to Linked List.

LeetCode 430. Flatten a Multilevel Doubly Linked List的更多相关文章

  1. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  2. LeetCode 430. Faltten a Multilevel Doubly Linked List

    题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...

  3. [LC] 430. Flatten a Multilevel Doubly Linked List

    You are given a doubly linked list which in addition to the next and previous pointers, it could hav ...

  4. 430. Flatten a Multilevel Doubly Linked List

    /* // Definition for a Node. class Node { public: int val = NULL; Node* prev = NULL; Node* next = NU ...

  5. [LeetCode] Flatten a Multilevel Doubly Linked List 压平一个多层的双向链表

    You are given a doubly linked list which in addition to the next and previous pointers, it could hav ...

  6. LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List

    您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁平化列表,使所有结点 ...

  7. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  8. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

  9. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

随机推荐

  1. 使用jquery.jqprint.js 实现的打印功能,IE9不能进行打印预览、火狐打印空白界面

    提示的内容:SCRIPT438: 对象不支持“ExecWB”属性或方法 首先解决IE9不能打印预览的问题: 查找了一大推资料 ,有两种说法:一种是IE的安全性级别太高:一种是需要安装什么   微软we ...

  2. 为cloudstack搭建ceph文件系统

    1. 安装dell服务器,  raid卡需要采用直通模式(non-raid); 各磁盘独立运行.  网络依赖硬件不同,使用万兆网卡或者两个千兆网卡做bonding6.  2. 配置host map(1 ...

  3. 设计模式--迭代器模式C++实现

    迭代器模式C++实现 1定义 他提供一种方法访问一个容器对象中的各个元素,而不暴漏该对象内部细节 注:迭代器是为容器服务的.迭代器模式提供了遍历容器的方便性,容器只管理增减元素就好,需要遍历时交给迭代 ...

  4. mongo docker image

    mongo 保存压缩镜像 docker save -o ~/Desktop/mongo.tar mongo 7za a -mx=9 ~/Desktop/mongo.tar{.7z,} 导入或拉取镜像 ...

  5. IDEA的Tomcat配置Web的项目创建以及Servlet简单运行。

    相关软件: 1.IDEA编译器 2.JDK 3.Tomcat          (相关软件都可以到官网上下载,老表提示:不要下载最新版本因为不要做试验品)   IDEA的安装非常简单,找好安装的盘,n ...

  6. canvas图形的组合与裁切

    当两个或两个以上的图形存在重叠区域时,默认情况下一个图形画在前一个图像之上.通过指定图像globalCompositeOperation属性的值可以改变图形的绘制顺序或绘制方式,globalAlpha ...

  7. PyCharm在win10的64位系统安装实例

    搭建环境 1.win10_X64,其他Win版本也可以. 2.PyCharm版本:Professional-2016.2.3. 搭建准备 1.到PyCharm官网下载PyCharm安装包. 2.选择W ...

  8. C# 设计模式巩固 - 抽象工厂模式

    前言   继续工厂模式高级版,抽象工厂模式.抽象工厂模式其实是从工厂方法模式拓展而来.在实际的生活中一个工厂的产品不可能是单一的,肯定是多种系列的产品. 介绍 - 抽象工厂模式 定义:(摘自百度百科~ ...

  9. week13《java程序设计》第13次作业总结

    week13<java程序设计>第13次作业总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 答: 1.IP与端口:ip和域名相对应,可找 ...

  10. 对于get方法是否需要synchronized修饰

    具体用法没有总结,只是说明一个用法而已,对于以前个人理解出现的偏差 [问题描述] 对于一个计数功能的实现,获取值的方法是否需要加锁? [以前理解] 我只需要在进行累加的方法上进行加锁即可,这样保证其可 ...