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 ...
随机推荐
- day0203
day02 1.for i in range() --->用于设置for循环的迭代设置. ranage 也是一个前闭后开的. 2.random.randrange() --->随机产生给予 ...
- #第 12 篇:解锁博客侧栏,GoGoGo!
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 我们的博客侧边栏有四项内容:最新文章.归档.分类和标签云.这些内容相对比较固定和独立, ...
- OpenCV学习笔记(一)、VS2015+OpenCV-4.1.1环境配置(Windows10)
1.登陆OpenCV官网,点击打开Releases,会看到各个版本的OpenCV安装文件,选择自己想要的版本下载即可.下载网址:https://opencv.org/releases/ 2.配置环境变 ...
- PHP CURL根据详细地址获取腾讯地图经纬度
<?php $address = "广东省广州市天河区"; $point = getPoint($address); var_dump($point);//输出经纬度 /** ...
- 前端项目升级到React-router5中遇到的问题解决方案以及思路
我胡汉三有日子没回来写写文章了,最近一直再忙着将老项目升级,所以没时间来搞文章,今天突然感觉开了挂一样,爱因斯坦附体,把之前的bug都搞定了,在这里特意把升级中遇到的问题,记录下来,算是把这个坑填上. ...
- Spring框架核心知识介绍
一:spring框架介绍 1.spring框架是为了解决复杂的企业级应用而创建的, 使用Spring可以让简单的JavaBean实现之前只有EJB才能完成的事情.但是Spring不仅仅局限于服务器 ...
- Jedis操作Redis--SortedSet类型
/** * SortedSet(有序集合) * ZADD,ZCARD,ZCOUNT,ZINCRBY,ZRANGE,ZRANGEBYSCORE,ZRANK,ZREM,ZREMRANGEBYRANK,ZR ...
- Jedis操作Redis--String类型
/** * String(字符串) * APPEND,BITCOUNT,BITOP,BITFIELD,DECR,DECRBY,GET,GETBIT,GETRANGE,GETSET,INCR,INCRB ...
- redis之mq实现发布订阅模式
示例代码-github 概述 Redis不仅可作为缓存服务器,还可用作消息队列,本示例演示如何使用redis实现发布/订阅消息队列. 在Redis中,发布者没有将消息发送给特定订阅者的程序.相反,发布 ...
- HDU-6437 Videos
HDU-6437 题意:一天有n个小时,现在有m场电影,每场电影有一个愉悦值,有k个人,电影分2种类型A, B, 并且每一场电影只能一个人看, 一个人可以看无数次电影, 只要时间足够, 但是连续看同一 ...