LeetCode 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 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.
示例:
输入:
1---2---3---4---5---6--NULL
|
7---8---9---10--NULL
|
11--12--NULL
输出:
1-2-3-7-8-11-12-9-10-4-5-6-NULL
以上示例的说明:
给出以下多级双向链表:

我们应该返回如下所示的扁平双向链表:

解题思路:
这道题是典型的 DFS(深度优先搜索)型例题,并且给出了图解,只要了解过 DFS 的应该立即就能想到思路。
针对这道题简单说下:深度优先搜索 就像一棵树(二叉树)的前序遍历,从某个顶点(链表头节点)出发,自顶向下遍历,然后遇到顶点的未被访问的邻接点(子节点 Child),继续进行深度优先遍历,重复上述过程(递归),直到所有顶点都被访问为止。
其逻辑以示例输入为例:
1---2---3---4---5---6--NULL
|
7---8---9---10--NULL
|
11---12---NULL
从节点 1 开始遍历,当前遍历链表为:1---2---3---4---5---6--NULL
遇到邻接点 2,其子链表为:7---8---9---10--NULL
将子链表头节点 7 作为参数传入 DFS 函数,当前遍历链表为:7---8---9---10---NULL
继续遍历,遇到邻接点 8,其子链表为:11--12--NULL
将子链表头节点 8 作为参数传入 DFS 函数,当前遍历链表为:11--12---NULL
继续遍历,无邻接点,遍历结束,返回当前链表尾节点 12
改变邻接点 8 与子链表头节点 11 关系:7---8---11---12
连接返回值 尾节点 12 和邻接点的下一个节点 9: 7---8---11---12---9---10---NULL
继续遍历,无邻接点,遍历结束,返回当前链表尾节点 10
改变邻接点 2 与 子链表头节点 7 关系:1---2---7---8---11---12---9---10--NULL
连接返回值 尾节点 10 和邻接点的下一个节点 3: 1---2---7---8---11---12---9---10---3---4---5---6--NULL
继续遍历,无邻接点,遍历结束,返回当前链表尾节点 6
递归结束,返回头节点 1,链表为: 1---2---7---8---11---12---9---10---3---4---5---6--NULL
Java:
class Solution {
public Node flatten(Node head) {
dfs(head);
return head;
}
//深度优先搜索函数
private Node dfs(Node head) {
Node cur = head;
while (cur != null) {
if (cur.child != null) {
//改变当前节点与子节点的关系
Node next = cur.next;//记录暂存下一个节点
cur.next = cur.child;//当前节点与子链表头节点连接
cur.next.prev = cur;
//传递子链表头节点作为参数到 dfs
Node childLast = dfs(cur.child);//childLast获得返回值为子链表的尾节点
childLast.next = next;//子链表尾节点与暂存节点连接
if (next != null) next.prev = childLast;//暂存节点不为空就将其prev指向子链表尾节点
cur.child = null;//子链表置空
cur = childLast;//刷新当前节点,跳过子链表的遍历
}
head = cur;//头节点刷新为当前节点
cur = cur.next;//刷新当前节点
}
return head;//返回子链表的尾节点
}
}
Python3:
class Solution:
def flatten(self, head: 'Node') -> 'Node':
self.dfs(head)
return head
def dfs(self, head):
cur = head
while cur:
if cur.child:
next = cur.next
cur.next = cur.child
cur.next.prev = cur
childLast = self.dfs(cur.child)
childLast.next = next
if next: next.prev = childLast
cur.child = None
head = cur
cur = cur.next
return head
欢迎关注微.信公.众号:爱写Bug

LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List的更多相关文章
- Java实现 LeetCode 430 扁平化多级双向链表
430. 扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例 ...
- Leetcode 430.扁平化多级双向链表
扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁 ...
- LeetCode430 扁平化多级双向链表
您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁平化列表,使所有结点 ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- [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
原题链接在这里: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 ...
- 430. Flatten a Multilevel Doubly Linked List
/* // Definition for a Node. class Node { public: int val = NULL; Node* prev = NULL; Node* next = NU ...
- LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...
随机推荐
- SQL Server字符串函数STUFF的使用
前言: 最近有个实践练习是将学生报名数据表student中的[st_id]字段的第二个和第三个字符删除,然后在此位置插入新的字符串“200900”,其实就是替换的意思,生成新的字符串. STUFF 函 ...
- .net core入门-项目启动时报错:HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure
在打开Core的项目首页时,页面有时候会出现:HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure的错误,但是这里面看不出任何错误详情,这个时候 ...
- C# copy files from source directory to destination file and rename repeated files and does not override
static void CopyFiles() { string sourceDir = @"D:\C\ll"; string destDir = @"D:\LL&quo ...
- 利用Python突破验证码限制
一.实验说明 本实验将通过一个简单的例子来讲解破解验证码的原理,将学习和实践以下知识点: Python基本知识 PIL模块的使用 二.实验内容 安装 pillow(PIL)库: $ sudo apt- ...
- django6-orm进阶操作
1.创建django环境的脚本 在自定义脚本中操作orm ,但是自定义脚本中不具备django的环境 ###test.py 脚本,引入django的环境即可使用orm操作数据库import os if ...
- python中的随机数生成
结论先行 生成随机数: # salt = ''.join(random.choices(string.ascii_letters + string.digits, k=3)) salt = ''.jo ...
- 边框渐变背景色border-image: linear-gradient()
前言 前几天无意间听说了边框渐变背景色,网上查了下,没有发现与之有关的介绍,亲测之后和大家分享一下. 边框渐变背景色 写法 border-image: gradient top right botto ...
- Junit4模板
模板 MallApplicationTests import org.junit.runner.RunWith; import org.springframework.boot.test.contex ...
- 微服务(入门学习五):identityServer4+ocelot+consul实现简单客户端模式
简介 主要是采用identity Server4 和ocelot 加上consul 实现简单的客户端模式 开发准备 环境准备 下载并安装Consul具体请参考前几篇的内容 项目介绍 创建ocelot ...
- PHP代码篇(四)--将字符串按指定字符切割
说,我有一个中间接手的需求,什么叫中间接手呢,就是这个功能已经上线了,并且已经产生数据了,现在要对这个功能进行修改,所以既要满足当下开发的需求,又要兼容以前的功能.简单说来,就是我们有一个后台添加商品 ...