Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ Return _the_ `head` _of the flattened list. The nodes in the list must have **al

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/11/09/flatten11.jpg)
![](https://leetcode-images.github.io/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/flatten11.jpg)

**Input:** head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

**Output:** [1,2,3,7,8,11,12,9,10,4,5,6]

**Explanation:** The multilevel linked list in the input is shown. After flattening the multilevel linked list it becomes: ![](https://assets.leetcode.com/uploads/2021/11/09/flatten12.jpg)
**Explanation:** The multilevel linked list in the input is shown. After flattening the multilevel linked list it becomes: ![](https://leetcode-images.github.io/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/flatten12.jpg)

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/11/09/flatten2.1jpg)
![](https://leetcode-images.github.io/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/flatten21.jpg)

**Input:** head = [1,2,null,3]

**Output:** [1,3,2]

**Explanation:** The multilevel linked list in the input is shown. After flattening the multilevel linked list it becomes: ![](https://assets.leetcode.com/uploads/2021/11/24/list.jpg)
**Explanation:**

The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes:

![](https://leetcode-images.github.io/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/list.jpg)

**Example 3:**

**Input:** head = []

**Output:** []

**Explanation:** There could be empty list in the input.
**Explanation:** There could be empty list in the input.

**Constraints:**

Expand All @@ -45,16 +50,26 @@ Return _the_ `head` _of the flattened list. The nodes in the list must have **al

We use the multilevel linked list from **Example 1** above:

1---2---3---4---5---6--NULL | 7---8---9---10--NULL | 11--12--NULL
1---2---3---4---5---6--NULL
|
7---8---9---10--NULL
|
11--12--NULL

The serialization of each level is as follows:

[1,2,3,4,5,6,null] [7,8,9,10,null] [11,12,null]
[1,2,3,4,5,6,null]
[7,8,9,10,null]
[11,12,null]

To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:

[1, 2, 3, 4, 5, 6, null] | [null, null, 7, 8, 9, 10, null] | [ null, 11, 12, null]
[1, 2, 3, 4, 5, 6, null]
|
[null, null, 7, 8, 9, 10, null]
|
[ null, 11, 12, null]

Merging the serialization of each level and removing trailing nulls we obtain:

[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Return _the most frequent prime number **greater** than_ `10` _out of all the nu

**Example 1:**

**![](https://assets.leetcode.com/uploads/2024/02/15/south)**
**![](https://leetcode-images.github.io/g3001_3100/s3044_most_frequent_prime/south.png)**

**Input:** mat = [[1,1],[9,9],[1,1]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</c

The valid arrangements of pieces on the board are:

![](https://assets.leetcode.com/uploads/2024/12/25/4040example1.drawio)![](https://assets.leetcode.com/uploads/2024/12/25/untitled-diagramdrawio.png)
![](https://leetcode-images.github.io/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/untitled-diagramdrawio.png)

* In the first 4 arrangements, the Manhattan distance between the two pieces is 1.
* In the last 2 arrangements, the Manhattan distance between the two pieces is 2.
Expand All @@ -39,7 +39,7 @@ Thus, the total Manhattan distance across all valid arrangements is `1 + 1 + 1 +

The valid arrangements of pieces on the board are:

![](https://assets.leetcode.com/uploads/2024/12/25/4040example2drawio.png)
![](https://leetcode-images.github.io/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/4040example2drawio.png)

* The first and last arrangements have a total Manhattan distance of `1 + 1 + 2 = 4`.
* The middle two arrangements have a total Manhattan distance of `1 + 2 + 3 = 6`.
Expand Down