【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 ...
随机推荐
- error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
- When should we write our own assignment operator in C++?
The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...
- List如何一边遍历,一边删除?
1.新手常犯的错误 可能很多新手(包括当年的我,哈哈)第一时间想到的写法是下面这样的: public static void main(String[] args) { List<String& ...
- Service Worker的应用
Service Worker的应用 Service worker本质上充当Web应用程序.浏览器与网络(可用时)之间的代理服务器,这个API旨在创建有效的离线体验,它会拦截网络请求并根据网络是否可用来 ...
- h5文件下载
// type1 await getFile(fileUrl).then((res) => { console.log('download',res); let bFile = window.U ...
- Windows查看端口被占用的程序!
"到底是谁占用了80端口,该怎么查,怎么终止它?",这里就简单讲解一下,在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等. 这里主要是用到windows下的 ...
- Git remote 远程仓库链接管理
SVN 使用单个集中仓库作为开发人员的通信枢纽,通过在开发人员的工作副本和中央仓库之间传递变更集来进行协作. 这与 Git 的分布式协作模型不同,后者为每个开发人员提供了自己的仓库副本,并具有自己的本 ...
- 创建项目文件(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 按照张同学和董同学的说法,创建项目文件首选是利用可以参照的项目计划模板,如果找不到,那就利用现有的项目文件,实在这些都没有 ...
- 当动态桌面遇上 HTML5
DreamScene2 + HTML5 = 无限可能.时隔一周,DreamScene2 动态桌面经过几个小版本的迭代,修复了一些问题并且功能也得到了增强.欢迎 Star 和 Fork https:// ...
- 获取登录验证码失败及前后端不同域导致session丢失问题分析记录
前言 前两周在把兄弟公司的几个服务部署到我们公司测试环境服务器的时候又遇到了不少问题,因为是前后端分离的项目,所以这次也同样遇到了跨域问题,解决方式也跟上一回的不一样,这里就再来分析记录一下. 登录验 ...