当前位置:网站首页>[leetcode] flat multi-level bidirectional linked list
[leetcode] flat multi-level bidirectional linked list
2022-06-11 01:50:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
- Multi level flat list
https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/ - analysis
The description of the topic is rather complicated , But after understanding it , The general meaning is to traverse the subset linked list first , Traverse backward ; Basically, you can think of using recursion , Judge whether the node has child, There is a recursive call to itself , If not, go straight down . After recursion , Insert the results into the previous linked list . - Realization
public Node flatten(Node head) {
Node node = head, next;
while (node != null) {
next = node.next;
if (node.child != null) {
Node child = flatten(node.child);
// find child Last element of
Node temp = child;
while (temp.next != null) {
temp = temp.next;
}
// take child Insert node Back
temp.next = next;
if (next != null) {
// next May is empty , And no longer need to maintain its prev
next.prev = temp;
}
node.next = child;
child.prev = node;
// Disconnect the child
node.child = null;
}
node = next;
}
return head;
}
LeetCode Time consuming :0ms
- Optimize
After that, I read the following explanation , You can remove the last node of the list every time you look for it , Enable another method to use recursion , It returns the last node of the linked list after each processing
public Node flatten02(Node head) {
// Optimize : Restart a method to start recursion , Return it to the last node after processing , Facilitate splicing ;; Avoid the same as above , Find the last node from the beginning
dfs(head);
return head;
}
private Node dfs(Node head) {
Node node = head, next, last = null;
while (node != null) {
next = node.next;
if (node.child != null) {
Node childLast = dfs(node.child);
// childLast yes child Last element of
// take child Insert node Back
childLast.next = next;
if (next != null) {
// next May is empty , And no longer need to maintain its prev
next.prev = childLast;
}
node.next = node.child;
node.child.prev = node;
// After that , The last node of the linked list on the splice is childLast
last = childLast;
// Disconnect the child
node.child = null;
} else {
last = node;
}
node = next;
}
return last;
}
It's all the same , You just need to find the tail node of the linked list and return
LeetCode Time consuming :0ms
summary
Something like this involves deep traversal , Consider using recursion to handle , Think about what will be returned after recursion , Got the return result , What needs to be done with the current element . Finally, just pay attention to the details .Last
LeetCode Special topic of linked list , Completed 31. Next month, we will start the next topic , Binary tree ~
边栏推荐
- Makefile:1860: recipe for target ‘cmake_ check_ build_ system‘ failed make: *** [cmake_check_build_syst
- LeetCode 1609 Even Odd Tree (bfs)
- 神经网络极简史,神经网络知识点整理
- [leetcode] reverse linked list
- 2022.6.6-----leetcode.732
- MATLAB随机函数汇总
- 1.7 calibration of Px4 remote controller
- Leetcode 430 flat a multilevel double linked list (DFS linked list)
- 2021-07-18 ROS笔记-基础和通讯
- Application of object storage S3 in distributed file system
猜你喜欢

2021-2-14 gephi学习笔记

Using MySQL database in nodejs

2021-02-27image processing of MATLAB

Multi interest recall model practice | acquisition technology

Leetcode 430 flat a multilevel double linked list (DFS linked list)

1.5、PX4载具选择

Leetcode linked list queue stack problem

flutter 状态管理

2021-2-26 compilation of programming language knowledge points

逻辑漏洞 / 业务漏洞
随机推荐
LeetCode 1749 Maximum Absolute Sum of Any Subarray (dp)
flutter 状态管理
2021-2-26 compilation of programming language knowledge points
2021-2-14 gephi learning notes
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
Leetcode 1248 count number of nice subarrays
Conda安装Pytorch后numpy出现问题
Bad RequestThis combination of host and port requires TLS.
detectron2训练自己的数据集和转coco格式
ros缺包怎么办
[Li mu] how to read papers [intensive reading of papers]
Record the packaging of the googlechrome browser plug-in
Role of handlermethodargumentresolver + use case
Uninstall mavros
SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)
Kubernetes binary installation (v1.20.15) (VII) plug a work node
Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software
[ROS introduction] cmakelist Txt and packages XML interpretation
LeetCode 1024 Video Stitching (dp,jump game)
[VBA Script] extract the information and pending status of all annotations in the word document