430. Flatten a Multilevel Doubly Linked List
/*
// 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的更多相关文章
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...
- [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 ...
- LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...
- [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 ...
- LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List
您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁平化列表,使所有结点 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- flash系统奔溃的主要原因
1.内存泄露(内存超过系统允许的最大限制,11.4版本为2G) 2.脚本死循环 3.舞台内元件的大小超出了系统限制
- Sass基础(一)
一.Sass语法规范 1.Sass老版本:通过tab键严格控制缩进:不带有任何分号和大括号:以“.sass”为扩展名. 2.Sass新语法格式(Scss):外形和css无差,以“.scss”为扩展名. ...
- Android Studio 小技巧(2):AS中Button文字默认大写的问题
问题类型 设置Layout中添加一个Button <Button android:id="@+id/bt_showerror" android:layout_width=&q ...
- C++ Knowledge series Inheritance & RTTI & Exception Handling
Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...
- 【从业余项目中学习1】C# 实现XML存储用户名密码(MD5加密)
最近在写一个C#的项目,用户需求是实现Winform的多文档界面与Matlab算法程序之间的交互.做了一段时间发现,这既能利用业余时间,实战中也可学习一些技术,同时刚毕业也增加一份收入.所以后面会不断 ...
- python+selenium第一个脚本
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport tim ...
- Movideo SaaS解决方案
类型: 定制服务 软件包: media solution collateral 联系服务商 产品详情 解决方案 概要 Movideo为媒体客户提供的SaaS解决方案部署在位于全球数据中心的Azure云 ...
- Help for enable SSL 3.0 and disable TLS 1.0..
https://support.mozilla.org/en-US/questions/967266 i cant find tab Encryption for enable SSL 3.0 and ...
- Python扩展包
Python扩展包 1.NumPy NumPy提供了多种python本身不支持的多种集合,有list.ndarray和ufunc. list 更加灵活的数组,支持多维,数据可不同型,存储数量远大于ar ...
- Js arguments.callee();函数自己调用自己
1.阶乘的时候,函数一般要用到递归算法,所以函数内部一定会调用自身 //递归,阶乘 function sum(num){ ) { ; } else{ ); //自己调用自己,递归 } } alert( ...