mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	国外版
This commit is contained in:
		
							
								
								
									
										78
									
								
								算法题(国外版)/flatten-a-multilevel-doubly-linked-list.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								算法题(国外版)/flatten-a-multilevel-doubly-linked-list.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,78 @@
 | 
			
		||||
<p>You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional <strong>child pointer</strong>. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a <strong>multilevel data structure</strong> as shown in the example below.</p>
 | 
			
		||||
 | 
			
		||||
<p>Given the <code>head</code> of the first level of the list, <strong>flatten</strong> the list so that all the nodes appear in a single-level, doubly linked list. Let <code>curr</code> be a node with a child list. The nodes in the child list should appear <strong>after</strong> <code>curr</code> and <strong>before</strong> <code>curr.next</code> in the flattened list.</p>
 | 
			
		||||
 | 
			
		||||
<p>Return <em>the </em><code>head</code><em> of the flattened list. The nodes in the list must have <strong>all</strong> of their child pointers set to </em><code>null</code>.</p>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<p><strong>Example 1:</strong></p>
 | 
			
		||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/09/flatten11.jpg" style="width: 700px; height: 339px;" />
 | 
			
		||||
<pre>
 | 
			
		||||
<strong>Input:</strong> head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
 | 
			
		||||
<strong>Output:</strong> [1,2,3,7,8,11,12,9,10,4,5,6]
 | 
			
		||||
<strong>Explanation:</strong> The multilevel linked list in the input is shown.
 | 
			
		||||
After flattening the multilevel linked list it becomes:
 | 
			
		||||
<img src="https://assets.leetcode.com/uploads/2021/11/09/flatten12.jpg" style="width: 1000px; height: 69px;" />
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p><strong>Example 2:</strong></p>
 | 
			
		||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/09/flatten2.1jpg" style="width: 200px; height: 200px;" />
 | 
			
		||||
<pre>
 | 
			
		||||
<strong>Input:</strong> head = [1,2,null,3]
 | 
			
		||||
<strong>Output:</strong> [1,3,2]
 | 
			
		||||
<strong>Explanation:</strong> The multilevel linked list in the input is shown.
 | 
			
		||||
After flattening the multilevel linked list it becomes:
 | 
			
		||||
<img src="https://assets.leetcode.com/uploads/2021/11/24/list.jpg" style="width: 300px; height: 87px;" />
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p><strong>Example 3:</strong></p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
<strong>Input:</strong> head = []
 | 
			
		||||
<strong>Output:</strong> []
 | 
			
		||||
<strong>Explanation:</strong> There could be empty list in the input.
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<p><strong>Constraints:</strong></p>
 | 
			
		||||
 | 
			
		||||
<ul>
 | 
			
		||||
	<li>The number of Nodes will not exceed <code>1000</code>.</li>
 | 
			
		||||
	<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
 | 
			
		||||
</ul>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<p><strong>How the multilevel linked list is represented in test cases:</strong></p>
 | 
			
		||||
 | 
			
		||||
<p>We use the multilevel linked list from <strong>Example 1</strong> above:</p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
 1---2---3---4---5---6--NULL
 | 
			
		||||
         |
 | 
			
		||||
         7---8---9---10--NULL
 | 
			
		||||
             |
 | 
			
		||||
             11--12--NULL</pre>
 | 
			
		||||
 | 
			
		||||
<p>The serialization of each level is as follows:</p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
[1,2,3,4,5,6,null]
 | 
			
		||||
[7,8,9,10,null]
 | 
			
		||||
[11,12,null]
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p>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:</p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
[1,    2,    3, 4, 5, 6, null]
 | 
			
		||||
             |
 | 
			
		||||
[null, null, 7,    8, 9, 10, null]
 | 
			
		||||
                   |
 | 
			
		||||
[            null, 11, 12, null]
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p>Merging the serialization of each level and removing trailing nulls we obtain:</p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
 | 
			
		||||
</pre>
 | 
			
		||||
		Reference in New Issue
	
	Block a user