LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接在这里: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的更多相关文章
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...
- [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 ...
- 430. Flatten a Multilevel Doubly Linked List
/* // Definition for a Node. class Node { public: int val = NULL; Node* prev = NULL; Node* next = NU ...
- [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 ...
- LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List
您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁平化列表,使所有结点 ...
- [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 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- [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 ...
随机推荐
- git branch 新建,推送与删除
在开发的许多时候我们都需要使用git提供的分支管理功能. 1.新建本地分支:git checkout -b test 新建一个名为:test 的本地分支. 2.提交本地分支:git push ori ...
- [Spring]Spring Mvc实现国际化/多语言
1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...
- 为cloudstack搭建ceph文件系统
1. 安装dell服务器, raid卡需要采用直通模式(non-raid); 各磁盘独立运行. 网络依赖硬件不同,使用万兆网卡或者两个千兆网卡做bonding6. 2. 配置host map(1 ...
- 你真的掌握 LVS、Nginx 及 HAProxy 的工作原理吗
你真的掌握 LVS.Nginx 及 HAProxy 的工作原理吗 当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web ...
- 各种数据库对应的jar包、驱动类名和URL格式
1.1. 各种数据库对应的jar包 具体如下: 数据库类型 对应的Jar文件 Oracle 8i classes12.zip 或 ojdbc14.jar Sybase jconn2.jar ...
- mac下通过brew切换php版本
第一步,先安装 brew Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及 Patch,可以方便的安装开发工具. Mac 自带ruby 所以安装起来很 ...
- Java进阶3. 内存回收机制
Java进阶3. 内存回收机制 20131029 前言: 学过C++的都知道,C++中内存需要程序员自己维护.说道这里,很多开发的同学就感觉很痛苦,当他转向Java的时候,就会说你看Java多好啊,程 ...
- CUDA库函数module management
http://horacio9573.no-ip.org/cuda/group__CUDA__MODULE_ga52be009b0d4045811b30c965e1cb2cf.html
- 201621123005《Java程序设计》第二周学习总结
201621123005<JAVA程序设计>第二周学习总结 1. 本周学习总结 本章学习了String 的不可变性.自动装箱和拆箱过程,并熟悉了动态数组等 Java中的应用,还有Array ...
- 初次使用Quartus II 13.0的疑惑及解决方法
初次接触Quartus II 13.0,遇到了很多的问题,把问题总结如下: 1.Quartus II 13.0的安装及破解 下载地址:http://t.cn/Rh2TFcz,密码是:g3gc (参考贴 ...