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.

/*
// Definition for a Node.
class Node {
public int val;
public Node prev;
public Node next;
public Node child;
};
*/
class Solution {
public Node flatten(Node head) {
if (head == null) {
return head;
}
Node cur = head;
while (cur != null) {
if (cur.child != null) {
Node right = cur.next;
cur.next = flatten(cur.child);
cur.next.prev = cur;
cur.child = null; while (cur.next != null) {
cur = cur.next;
}
if (right != null) {
cur.next = right;
cur.next.prev = cur;
}
}
cur = cur.next;
}
return head;
}
}

[LC] 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. Flatten a Multilevel Doubly Linked List

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

  3. 430. Flatten a Multilevel Doubly Linked List

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

  4. LeetCode 430. Faltten a Multilevel Doubly Linked List

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

  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. [LC] 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 ...

  8. [LeetCode] 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 ...

  9. [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 ...

随机推荐

  1. loback.xml 在idea中代码自动完成

    1.下载xsd文件 2.idea添加xsd文件 URI: http://ch.qos.logback/xml/ns/logback File: D:\env\plugins\logback\logba ...

  2. HashMap源码阅读笔记

    HashMap源码阅读笔记 本文在此博客的内容上进行了部分修改,旨在加深笔者对HashMap的理解,暂不讨论红黑树相关逻辑 概述   HashMap作为经常使用到的类,大多时候都是只知道大概原理,比如 ...

  3. redis(三)----连接池配置

    1. 目录结构: 2. 测试源码 package com.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.Jed ...

  4. Lock wait timeout exceeded; try restarting transaction(mysql事务锁)

    现场环境客户要求删数据(界面没法直接操作),于是直接在数据库进行查询删除了,删完发现界面依然能查到删除后的数据,又用sql语句进行删除,发现报了错:Lock wait timeout exceeded ...

  5. kube-proxy详解

    KUBE_LOGTOSTDERR="--logtostderr=true"KUBE_LOG_LEVEL="--v=4"NODE_HOSTNAME="- ...

  6. js数组全等

    js 数组全等(对象) if(this.eqOrNotEq(arr)){} eqOrNotEq(arr) { return !arr.some(function(value, index) { ret ...

  7. hdu 3483 矩阵乘法

    这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...

  8. 查看电脑连接的WiFi的密码

    这里提供两种办法:图形界面操作版.命令行操作版 方法一: 打开控制面板 点击红色框部分 方法二 打开命令行:输入命令netsh wlan show profiles "连接的WiFi的名称& ...

  9. 药物动力学|肿瘤药物基因组研究的策略|OMIM database|PharmGKB

    生命组学 同义突变虽然不改变蛋白质种类,但是影响量,修饰的稳定性. SNP vs mutation SNV单核苷酸变化,mutation,SNP是从群体角度思考的,约有1%,mutation比SNP还 ...

  10. java数据库连接池比较

    dbcp dbcp可能是使用最多的开源连接池,原因大概是因为配置方便,而且很多开源和tomcat应用例子都是使用的这个连接池吧.这个连接池可以设置最大和最小连接,连接等待时间等,基本功能都有.这个连接 ...