Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eaea291

Browse files
authored
Update 5.4.5展平链表.md
1 parent d73aa57 commit eaea291

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

‎5.4.5展平链表.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
- 问题:
2-
- 给定一个链表的头节点,要求展平一个双向链表。这意味着将链表中的嵌套结构(比如 **`child`** 指针)转换为一个单一的链表。
2+
- 给定一个链表的头节点,要求展平一个双向链表。这意味着将链表中的嵌套结构(比如 **`child`** 指针)转换为一个单一的链表。每个节点都是一个结构体,定义如下所示:
3+
4+
```cpp
5+
struct Node{
6+
Node* next;
7+
Node* prev;
8+
Node* child;
9+
int value;
10+
};
11+
```
12+
313
- 例子:
414
- 原输入:
515
@@ -15,7 +25,17 @@
1525
1 -> 4 -> 5 -> 2 -> 3
1626
```
1727
18-
- 算法思想:前序遍历
28+
- 问题分析:
29+
1. 存在嵌套结构: 题目中的 `child` 指针,说明存在链表内部的嵌套结构;
30+
2. 整体与局部的关系: 嵌套的子链表结构与整体链表结构相似,可以将展平问题分解为先展平子链表,再将其连接到父链表的过程。
31+
3. 逐步简化问题: 在展平链表的问题中,可以通过逐一处理每个节点及其子链表来简化问题。
32+
1. 处理每一个节点:
33+
1. 情况一,为空,则终止
34+
2. 情况二,不为空,则将这个节点加入到结果列表里
35+
1. 检查节点是否有child,如果有,则继续检查子child
36+
2. 检查节点是否有next 节点,如果有,则继续检查next child
37+
4. 终止条件: 链表为空或没有子链表的节点。
38+
- 算法思想:递归+前序遍历
1939

2040
```cpp
2141
#include <iostream>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /