Skip to content

Commit 9523d8e

Browse files
authored
Create Readme.md
1 parent 28944a5 commit 9523d8e

File tree

1 file changed

+25
-0
lines changed
  • Linked_List/430.Flatten-a-Multilevel-Doubly-Linked-List

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
### 430.Flatten-a-Multilevel-Doubly-Linked-List
2+
3+
本题的基本思想是,对于一个节点h,我们需要依次拼接h本身、遍历h->child得到的链表,遍历h->next得到的链表。考虑到第三部分要接在第二部分的末尾,所以这个“遍历”的递归函数的返回值应该是整个链表的最后一个节点。
4+
5+
所以,当h的next和child都存在的时候,大致的思路应该是这样的:
6+
```cpp
7+
Node* dfs(Node* h)
8+
{
9+
child = h->child;
10+
next = h->next;
11+
Node* childEnd = dfs(child);
12+
Node* nextEnd = dfs(next);
13+
14+
h->next = child; // h和child拼接
15+
child->prev = h;
16+
childEnd->next = next; //childEnd与next拼接
17+
next->prev = childEnd;
18+
19+
return nextEnd;
20+
}
21+
```
22+
但是在实际处理的时候,考虑当child和next各自是否为NULL时,具体的代码写法会不一样。
23+
1. child和next都为空:直接返回h
24+
2. child不为空,next为空:将h与child拼接后,返回的是childEnd
25+
3. child为空,next不为空:返回的是nextEnd.

0 commit comments

Comments
 (0)