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. vue中的axios请求

    1.get请求 // get this.$axios.get('请求路径') .then(function (response) { console.log(response); // 成功 }) . ...

  2. POJ 2676:Sudoku 数独

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15830   Accepted: 7737   Special ...

  3. mysql时间类型总结及常用时间函数

    日期时间和类型 常用日期和时间类型 字节 year                1       表示年份                   值范围:(1901----2155) date     ...

  4. VS2017中使用ReportViewer控件,vs2017找不到Microsoft Rdlc Report Designer for Visual Studio

    VS2017中没有ReportViewer控件,这个控件用来实现在项目中显示和打印关系数据库中的表比较容易,特别是想要打印的时候,这个比用DataGridView和PrintDocument要简单一些 ...

  5. Ubuntu Hadoop使用过程中的一些技巧1

    权限不足:打开有管理员权限的文件夹:sudo nautilus  输入密码即可进入最高权限的文件管理界面可以快速对文件进行修改删除操作 修改权限:chmod命令     chmod -R 777 文件 ...

  6. nodejs(11)Express 中进行数据库操作

    配置 MySql 数据库环境 mysql 第三方模块的介绍和基本配置 要安装操作数据库的第三方包npm i mysql -S 导入 包 const mysql = require('mysql') 创 ...

  7. bitcoin-cli

    == Blockchain ==getbestblockhashgetblock "blockhash" ( verbosity ) getblockchaininfogetblo ...

  8. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  9. Python 重新加载模块

    每个Python文件中的import modulename只被加载一遍,如果在运行过程中,这个Module被更改了,即使在在interpretor中运行import 语句也没用. 可以使用import ...

  10. Maven--mirror 和 repository

    参考:http://blog.csdn.net/isea533/article/details/22437511   http://www.cnblogs.com/xdouby/p/6502925.h ...