原题链接在这里: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. angular中的cookies与cookieStore区别

    设置cookie用put()方法: $cookies.put(key, value[, options]); $cookieStore.put(key, value); 例如设置一个cookie,名为 ...

  2. IDEA 中使用 Vue 提示 namespace is not bound

    今天在 IDEA 中使用 vue.js 时提示我如下错误信息 解决方法: 把这个校验项目去掉就可以了.

  3. js中的内部属性与delete操作符

    本文正式地址:http://www.xiabingbao.com/javascript/2015/08/03/javascript-delete-configurable 在讲解Configurabl ...

  4. iOS 和Android客户端测试区别整理ing

    区别很多,慢慢发现整理,注重细节,避免遗漏 消息推送区别: 1.推送渠道: 1.1 iOS走iOS自带的渠道进行系统内推送,应用内和应用外推送无明显差别,均可以收到push信息. 1.2 安卓由于谷歌 ...

  5. Codeforces Round #412

    第一题水题,8分钟1a #include<map> #include<set> #include<cmath> #include<queue> #inc ...

  6. 搞懂分布式技术9:Nginx负载均衡原理与实践

    搞懂分布式技术9:Nginx负载均衡原理与实践 本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容, ...

  7. simple HTTP server with upload

    #!/usr/bin/env python """Simple HTTP Server With Upload. https://github.com/tualatrix ...

  8. IOS-APP主流UI框架结构

    一.简单示例 说明:使用APP主流UI框架结构完成简单的界面搭建 搭建页面效果:                                二.搭建过程和注意点 1.新建一个项目,把原有的控制器删 ...

  9. MySQL pt-table-checksum及pt-table-sync校验及修复主从一致性

    [pt-table-checksum]pt-table-checksum是percona-toolkit系列工具中的一个, 可以用来检测主. 从数据库中数据的一致性.其原理是在主库上运行, 对同步的表 ...

  10. 如何理解CMDB的套路

    CMDB成功和失败,关于掌握的CMDB套路的多与少.深与浅! 前几天在对一个项目进行总结,编写CMDB的配置管理规范,发现还是有很多套路,本文就是老王总结的CMDB套路! 套路1:CMDB名字应该改一 ...