/*
// 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. Python常用模块(一)

    一.time模块 time模块提供各种操作时间的函数 时间三种格式 1.时间戳 以1970年1月1日 00:00:00开始的秒数 2.本地时间 localtime,表示计算机当前的时间 3.UTC世界 ...

  2. android 扇形菜单

    引言: android中的菜单与windows的菜单没有什么区别,基本就是一个矩形框,如下: 这个菜单有多么能经得住历史的考验我就不多说了!我们再来看看最新有关手机可操作区域的调查 有此可以看出屏幕越 ...

  3. 用代码初始化AE控件许可

    当AE二次开发与其他平台一起使用的时候,有时候会不方便加LicenseControl控件,需要用代码初始化许可: 在构造函数中初始化许可: public Form1() { InitializeCom ...

  4. 在sql中如何把一列的值拆分成多列

  5. 计算结构体、数组、指针的sizeof

    1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...

  6. GitLab-Runner 安装配置

    https://docs.gitlab.com/runner/install/linux-repository.html 直接看官方教程 systemctl status gitlab-runner. ...

  7. Selenium入门系列3 单个元素的定位方法

    UI自动化首先要识别对象,再操作对象,最后判定实际结果与预期结果是否一致. 这一节学习的是识别单个对象,webdriver提供了8种方式. <a id="idofa" cla ...

  8. IOS 设置定时器,执行方法

    //设置定时器(1秒后跳到一下题) [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:1.0];

  9. Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)

    https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...

  10. iis 发布失败原因总结

    3篇文章 1. https://www.cnblogs.com/adzhouyang/p/7357086.html 2..https://blog.csdn.net/li_ser/article/de ...