当前位置:网站首页>剑指 Offer II 028. 展平多级双向链表
剑指 Offer II 028. 展平多级双向链表
2022-06-25 12:19:00 【小白码上飞】
概要
借助栈来保存有子链表节点的next节点
题目
多级双向链表中,除了指向下一个节点和前一个节点指针之外,它还有一个子链表指针,可能指向单独的双向链表。这些子列表也可能会有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。
给定位于列表第一级的头节点,请扁平化列表,即将这样的多级双向链表展平成普通的双向链表,使所有结点出现在单级双链表中。

链接:https://leetcode.cn/problems/Qv1Da2
思路
题目好复杂,看瞎了。
屡一下思路,实际上就是从头结点开始遍历,如果有子链表,如果子链表遍历完毕,依照前指针去找自己的父节点,将父节点的next节点挂到新生成链表的结尾。当然,这里还要维护前节点的指针。
整个操作都很麻烦呀。
我们可以借助栈,一旦遇到了存在子链表的节点,则将next节点压入栈。一旦一个子链表走到头,出栈,并将栈中的节点挂到子链表最后一个节点后面,并将出栈节点的前指针指向最后一个节点。
解法:借助栈来保存有子链表节点的next节点
代码
public Node flatten(Node head) {
Stack<Node> stack = new Stack<>();
Node dummy = new Node();
dummy.next = head;
while (head != null) {
if (head.child != null) {
// 下一个节点为空的话,则不需要入栈
if (head.next != null) {
stack.push(head.next);
}
// 这里要注意处理前后指针
head.next = head.child;
head.child.prev = head;
Node temp = head.child;
// 因为只要单纯的双向链表,因此child也要去掉
head.child = null;
head = temp;
continue;
}
if (head.next != null) {
head = head.next;
} else if (!stack.isEmpty()) {
Node pop = stack.pop();
// 这里要注意处理前后指针
head.next = pop;
pop.prev = head;
head = pop;
} else {
break;
}
}
return dummy.next;
}
边栏推荐
- Initialize the project using the express framework
- Why are databases cloud native?
- First acquaintance with CANopen
- Jupyter notebook theme font setting and automatic code completion
- Maximum number [abstract rules for abstract sorting]
- Laravel is not logged in and cannot access the background by entering the URL
- Array reorder based on a field
- Select randomly by weight [prefix and + dichotomy + random target]
- Oracle trigger error report table or view does not exist
- (3) Pyqt5 tutorial -- > signal and slot preliminary test
猜你喜欢

Shell learning notes (latest update: 2022-02-18)

Geospatial search: implementation principle of KD tree

Elemntui's select+tree implements the search function

CUDA error: unspecified launch failure

AI assisted paper drawing of PPT drawing

MySQL and excel tables importing database data (Excel for MySQL)

Using CMD (command prompt) to install MySQL & configure the environment
![Select randomly by weight [prefix and + dichotomy + random target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
Select randomly by weight [prefix and + dichotomy + random target]

二叉树之_哈夫曼树_哈弗曼编码

微信全文搜索技术优化
随机推荐
Three jobs! You can learn this from me (attached with graduation vlog)
架构师必备的七种能力
[AI helps scientific research] fool drawing of loss curve
线上服务应急攻关方法论
Negative sample image used in yolov5 training
Flutter automatically disappears after receiving push
Elemtnui select control combined with tree control to realize user-defined search method
Micro engine generates QR code
visual studio2019链接opencv
Differences between JS and JQ operation objects
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1
STM32 stores float data in flash
Resolution of PPT paper drawing
515. Find Largest Value in Each Tree Row
2021-09-22
1251- Client does not support authentication protocol MySql错误解决方案
GPS receiver design (1)
Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
Jupyter notebook theme font setting and automatic code completion
node. JS architecture optimization: reverse proxy and cache service