/*
// Definition for a Node.
class Node {
public:
int val = NULL;
Node* prev = NULL;
Node* next = NULL;
Node* child = NULL; Node() {} 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;
flatten2(head);
return head;
}
Node* flatten2(Node* head) { // flatten and return tail
Node* ret = head;
while (head) {
ret = head;
if (head->child) {
Node* tail = flatten2(head->child);
tail->next = head->next;
if (tail->next)
tail->next->prev = tail;
head->next = head->child;
head->next->prev = head;
head->child = NULL;
head = tail->next;
ret = tail;
}
else {
head = head->next;
}
}
return ret;
}
};

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

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

  9. Flatten Binary Tree to Linked List [LeetCode]

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

随机推荐

  1. Kindeditor单独调用多图上传

    html代码: <input type="button" id="J_selectImage" value="图片上传" />& ...

  2. RabbitMQ双向发送(接收端有返回RPC模式)

    remote procedure call 服务端 import pika import time connection = pika.BlockingConnection(pika.Connecti ...

  3. SPFieldLookupValue

    //得到查阅项的值SPWeb web = site.OpenWeb();SPList list = web.Lists["DemoList"];SPListItem item = ...

  4. ansible使用9-Playbooks: Special Topics

    Accelerated Mode port 5099 持续连接30min --- - hosts: all accelerate: true tasks: - name: some task comm ...

  5. MySQL的基础(优化)1

    1,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽可能小 2,在可能的情况下,应该尽量把字段设置为NOT NULL,这样在将来执行查询的时候,数据库不用去比较NULL值 3,对于某 ...

  6. Linux远程桌面(三)

    上一篇中的vnc配置已经很方便了,这一篇介绍更为简单的xmanager的配置,xmanager画面好一丢丢. Xmanager服务器配置             Linux远程桌面(二):vnc之xi ...

  7. vs2008使用mysql链接错误

    原因是因为安装了64位的mysql,而开发工具室32位的,需要安装32位的开发库就可以了

  8. git/github初级运用自如(转自:虫师)

    注:本文来源于 虫师博客(http://www.cnblogs.com/fnng/archive/2012/01/07/2315685.html) ,内容详尽,真实有用. 另:发一个github使用教 ...

  9. 设定网页最小最大宽度和高度(兼容IE6)

    http://www.cnblogs.com/double-bin/archive/2011/12/19/2293093.html /* 最小寬度 */ .min_width{min-width:30 ...

  10. IOS OAuth授权分析

    一.黑马微博 ---> 用户的微博数据1.成为新浪的开发者(加入新浪微博的开发阵营)* 注册一个微博帐号,登录http://open.weibo.com帐号:643055866@qq.com密码 ...