当前位置:网站首页>Sword finger offer II 028 Flatten multi-level bidirectional linked list
Sword finger offer II 028 Flatten multi-level bidirectional linked list
2022-06-25 12:59:00 【Small white yards fly up】
Summary
With the help of the stack to save the child linked list nodes next node
subject
In the multi-level bidirectional linked list , In addition to pointing to the next node and the previous node pointer , It also has a child list pointer , It may point to a separate double linked list . These sublists may also have one or more of their own , And so on , Generate multilevel data structures , As the following example shows .
Give the header node positioned at the first level of the list , Please flatten the list , That is to flatten such a multi-level two-way linked list into an ordinary two-way linked list , Make all nodes appear in a single level double linked list .

link :https://leetcode.cn/problems/Qv1Da2
Ideas
The topic is so complicated , Blind .
Think again and again , It is actually traversing from the beginning , If there is a sub linked list , If the sub linked list is traversed , Find your own parent node according to the previous pointer , The next The node is hung to the end of the newly generated linked list . Of course , Here, you also need to maintain the pointer of the previous node .
The whole operation is very troublesome .
We can use the stack , Once you encounter a node that has a child linked list , Will next Nodes are pushed onto the stack . Once a sub linked list comes to an end , Out of the stack , And hang the nodes in the stack behind the last node in the sub linked list , And point the front pointer of the stack node to the last node .
solution : With the help of the stack to save the child linked list nodes next node
Code
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 the next node is empty , No need to stack
if (head.next != null) {
stack.push(head.next);
}
// Here we need to pay attention to dealing with the front and back pointers
head.next = head.child;
head.child.prev = head;
Node temp = head.child;
// Because as long as the simple two-way linked list , therefore child And get rid of
head.child = null;
head = temp;
continue;
}
if (head.next != null) {
head = head.next;
} else if (!stack.isEmpty()) {
Node pop = stack.pop();
// Here we need to pay attention to dealing with the front and back pointers
head.next = pop;
pop.prev = head;
head = pop;
} else {
break;
}
}
return dummy.next;
}
边栏推荐
- And console Log say goodbye
- 地理空间搜索 ->R树索引
- Idea2017 how to set not to automatically open a project at startup
- Using CMD (command prompt) to install MySQL & configure the environment
- JS enter three integers a, B and C, and sort them from large to small (two methods)
- JS function exercises
- [machine learning] model and cost function
- Oracle backup or restore database (expdp, impdp)
- 顺序表的折半查找法
- Capabilities required by architects
猜你喜欢

药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质

2021-09-28

几分钟上线一个网站 真是神器

The editor is used every day. What is the working principle of language service protocol?

剑指offer 第 3 天字符串(简单)

Capabilities required by architects

Elemntui's select+tree implements the search function

3+1 guarantee: how is the stability of the highly available system refined?

Fedora 35 部署DNS主从和分离解析 —— 筑梦之路

My first experience of go+ language -- a collection of notes on learning go+ design architecture
随机推荐
Baidu search stability analysis story
Concat(), join(), reverse(), sort() method in JS array
MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
leetcode - 384. Scramble array
剑指 Offer 04. 二维数组中的查找
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
2021-09-30
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
Koa frame
JS array de duplication
JS uses the for loop in the function to insert and delete the array at the specified position
初识CANOpen
KVM 脚本管理 —— 筑梦之路
First acquaintance with CANopen
[flask tutorial] flask overview
list.replace, str.append
RESTful和RPC
mysql导入导出数据到excel表日期出现问题
始终保持疫情防控不放松 营造安全稳定的社会环境
JS picture switching (simple and practical)