LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List
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_iteration(Node *head) {
if (head == NULL) {
return NULL;
}
for (Node *p = head; p != NULL; p = p->next) {
Node *next = p->next;
if (p->child != NULL) {
p->next = p->child;
p->child->prev = p;
p->child = NULL;
Node *tmp = p->next;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = next;
next->prev = tmp;
}
}
return head;
}
// 解法二:递归
Node* flatten_recursion(Node *head) {
if (head == NULL) {
return head;
}
Node *p = head;
while (p != NULL) {
if (p->child != NULL) {
Node *next = p->next;
Node *nextLayer = flatten_recursion(p->child);
p->next = nextLayer;
nextLayer->prev = p;
p->child = NULL;
while (nextLayer->next != NULL) {
nextLayer = nextLayer->next;
}
nextLayer->next = next;
if (next != NULL) {
next->prev = nextLayer;
}
}
p = p->next;
}
return head;
}
};
LeetCode 430. Faltten a Multilevel Doubly Linked List的更多相关文章
- LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- [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 ...
- 430. Flatten a Multilevel Doubly Linked List
/* // Definition for a Node. class Node { public: int val = NULL; Node* prev = NULL; Node* next = NU ...
- [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] 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 ...
- [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...
随机推荐
- 关于window.location.href 传中文参数 乱码问题
传中文查询乱码问题 则需要对要传的参数进行二次编码 例如 window.location.href ="/xx.jsp?name="+name+""; 这样子 ...
- Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)
Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [Python] socket发送UDP广播实现聊天室功能
一.说明 本文主要使用socket.socket发送UDP广播来实现聊天室功能. 重点难点:理解UDP通讯流程.多线程.UDP广播收发等. 测试环境:Win10\Python3.5. 程序基本流程:创 ...
- 使用golang对海康sdk进行业务开发
目录 准备工作 开发环境信息 改写HCNetSDK.h头文件 开发过程 基本数据类型转换 业务开发 参考 项目最近需要改造升级:操作海康摄像头(包括登录,拍照,录像)等基本功能.经过一段时间研究后,发 ...
- 通过sql命令建表 和 主外键约束以及其他约束
create table命令 create table dept ( dept_id int primary key, dept_name ) not null, dept_address ) ) c ...
- 业务代码的救星——Java 对象转换框架 MapStruct 妙用
简介 在业务项目的开发中,我们经常需要将 Java 对象进行转换,比如从将外部微服务得到的对象转换为本域的业务对象 domain object,将 domain object 转为数据持久层的 dat ...
- 爬虫——网页解析利器--re & xpath
正则解析模块re re模块使用流程 方法一 r_list=re.findall('正则表达式',html,re.S) 方法二 创建正则编译对象 pattern = re.compile('正则表达式 ...
- NLP(六) 分块、句法分析、依存分析
内置分块器 分块:从文本中抽取短语 import nltk text = 'Lalbagh Botanical Garden is a well known botanical garden in B ...
- MSSql 保留两位小数的用法
--MSSql 保留两位小数的用法1: round(@sum,2),2: Convert(decimal(18,2),@sum) select round(9,2) ...