【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/
题目描述:
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.
Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.
Example:
Input:
1---2---3---4---5---6--NULL
|
7---8---9---10--NULL
|
11--12--NULL
Output:
1-2-3-7-8-11-12-9-10-4-5-6-NULL
Explanation for the above example:
Given the following multilevel doubly linked list:
We should return the following flattened doubly linked list:
题目大意
给出的是一个带有子节点的双向链表。要求把这个带有子节点的双向链表转化为一个不带子节点的双向链表,其规则是把子节点所有的节点都插入到该节点的后面。
解题方法
看到把子节点插入到后面,就想到了我们应该使用的是DFS,这种搜索方式会让我们提前使用更深层次的节点,当更深层次的搜索结束之后再往上层返回。
现在的思路就是每次遇到child节点,就把这个节点作为当前node的next节点;并且要遍历child节点后面的所有节点,找到child链表最后面的节点,作为要插入的一整段链表最后的节点,即原node.next节点prev节点。
做法需要新定义一个函数,这个函数对每个child链表进行遍历,把整段的child链表插入到原链表中。
思路总结就是:DFS负责查找,新定义的函数负责插入。
代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
"""
class Solution(object):
def flatten(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head: return None
node = head
while node:
node_next = node.next
if node.child:
flattened = self.flatten(node.child)
node.child = None
nextNode = self.appendToList(node, flattened)
node = nextNode
else:
node = node.next
return head
def appendToList(self, node, listToAppendHead):
next_node = node.next
node.next = listToAppendHead
listToAppendHead.prev = node
while node.next:
node = node.next
node.next = next_node
if next_node:
next_node.prev = node
return next_node
日期
2018 年 8 月 23 日 ———— 疲惫说明在逆流而上
【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)的更多相关文章
- LeetCode 430. Flatten a Multilevel Doubly Linked List
原题链接在这里:https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/ 题目: You a ...
- LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...
- [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 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- Leetcode:Flatten Binary Tree to Linked List 解题报告
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
随机推荐
- mysql_sql查性能语句
mysql> SHOW PROCESSLIST; +----+--------+----------------------+-------+-------------+--------+--- ...
- C++类成员初始化列表的构造顺序
看下面代码, 输出结果是多少呢? class A{ public: A(int k) : j(k), i(j) { } void show() { cout << this->i & ...
- mysql 多表关联查询
多个表右链接查询 名字,学校名称,学校类型,城市名称,国家地区 左链接查询 子查询 索引 #创建MySQL时添加索引 mysql> create table userIndex( id int ...
- 日常Java 2021/10/20
Java提供了一套实现Collection接口的标准集合类 bstractCollection 实现了大部分的集合接口. AbstractList 继承于AbstractCollection并且实现了 ...
- javaIO——输入输出流
字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...
- linux如何安装缺失依赖
这里要提到一个网站https://pkgs.org/,他是linux系统的一个相关网站,里面都是相关内容 Warning: RPMDB altered outside of yum. ** Found ...
- jquery总结和注意事项
1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...
- JavaScript实现数组去重方法
一.利用ES6 Set去重(ES6中最常用) function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'tru ...
- python安装imblearn(PackageNotFoundError: ''Package missing in current channels")
1.imblearn包在anaconda中是没有的,需要在命令行下自行安装,以下两个命令任选一个: 1. conda install -c glemaitre imbalanced-learn2. p ...
- 为什么volatile能保证有序性不能保证原子性
对于内存模型的三大特性:有序性.原子性.可见性. 大家都知道volatile能保证可见性和有序性但是不能保证原子性,但是为什么呢? 一.原子性.有序性.可见性 1.原子性: (1)原子的意思代表着-- ...