430:扁平化多级双向链表

This commit is contained in:
huangge1199 2021-06-07 11:03:13 +08:00
parent 8072745345
commit 8893cc5464
3 changed files with 225 additions and 0 deletions

View File

@ -22,6 +22,13 @@ public class Node {
this.random = random; this.random = random;
} }
public Node(int val, Node prev, Node next, Node child) {
this.val = val;
this.prev = prev;
this.next = next;
this.child = child;
}
public Node setHead(List<Integer> list, List<Integer> index) { public Node setHead(List<Integer> list, List<Integer> index) {
Node head = new Node(list.get(0)); Node head = new Node(list.get(0));
int i = 1; int i = 1;

View File

@ -0,0 +1,139 @@
//多级双向链表中除了指向下一个节点和前一个节点指针之外它还有一个子链表指针可能指向单独的双向链表这些子列表也可能会有一个或多个自己的子项依此类推
//成多级数据结构如下面的示例所示
//
// 给你位于列表第一级的头节点请你扁平化列表使所有结点出现在单级双链表中
//
//
//
// 示例 1
//
// 输入head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
//输出[1,2,3,7,8,11,12,9,10,4,5,6]
//解释
//
//输入的多级列表如下图所示
//
//
//
//扁平化后的链表如下图
//
//
//
//
// 示例 2
//
// 输入head = [1,2,null,3]
//输出[1,3,2]
//解释
//
//输入的多级列表如下图所示
//
// 1---2---NULL
// |
// 3---NULL
//
//
// 示例 3
//
// 输入head = []
//输出[]
//
//
//
//
// 如何表示测试用例中的多级链表
//
// 示例 1 为例
//
// 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]
//
//
// 为了将每一级都序列化到一起我们需要每一级中添加值为 null 的元素以表示没有节点连接到上一级的上级节点
//
// [1,2,3,4,5,6,null]
//[null,null,7,8,9,10,null]
//[null,11,12,null]
//
//
// 合并所有序列化结果并去除末尾的 null
//
// [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
//
//
//
// 提示
//
//
// 节点数目不超过 1000
// 1 <= Node.val <= 10^5
//
// Related Topics 深度优先搜索 链表
// 👍 199 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.Node;
//430:扁平化多级双向链表
public class FlattenAMultilevelDoublyLinkedList {
public static void main(String[] args) {
//测试代码
Solution solution = new FlattenAMultilevelDoublyLinkedList().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/*
// Definition for a Node.
class Node {
public int val;
public Node prev;
public Node next;
public Node child;
};
*/
class Solution {
public Node flatten(Node head) {
if (head == null) {
return head;
}
// pseudo head to ensure the `prev` pointer is never none
Node pseudoHead = new Node(0, null, head, null);
flattenDFS(pseudoHead, head);
// detach the pseudo head from the real head
pseudoHead.next.prev = null;
return pseudoHead.next;
}
/* return the tail of the flatten list */
public Node flattenDFS(Node prev, Node curr) {
if (curr == null) {
return prev;
}
curr.prev = prev;
prev.next = curr;
// the curr.next would be tempered in the recursive function
Node tempNext = curr.next;
Node tail = flattenDFS(curr, curr.child);
curr.child = null;
return flattenDFS(tail, tempNext);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,79 @@
<p>多级双向链表中,除了指向下一个节点和前一个节点指针之外,它还有一个子链表指针,可能指向单独的双向链表。这些子列表也可能会有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。</p>
<p>给你位于列表第一级的头节点,请你扁平化列表,使所有结点出现在单级双链表中。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
<strong>输出:</strong>[1,2,3,7,8,11,12,9,10,4,5,6]
<strong>解释:
</strong>
输入的多级列表如下图所示:
<img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/multilevellinkedlist.png" style="height: 363px; width: 640px;">
扁平化后的链表如下图:
<img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/multilevellinkedlistflattened.png" style="height: 80px; width: 1100px;">
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>head = [1,2,null,3]
<strong>输出:</strong>[1,3,2]
<strong>解释:
</strong>输入的多级列表如下图所示:
1---2---NULL
|
3---NULL
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>head = []
<strong>输出:</strong>[]
</pre>
<p>&nbsp;</p>
<p><strong>如何表示测试用例中的多级链表?</strong></p>
<p><strong>示例 1</strong> 为例:</p>
<pre> 1---2---3---4---5---6--NULL
|
7---8---9---10--NULL
|
11--12--NULL</pre>
<p>序列化其中的每一级之后:</p>
<pre>[1,2,3,4,5,6,null]
[7,8,9,10,null]
[11,12,null]
</pre>
<p>为了将每一级都序列化到一起,我们需要每一级中添加值为 null 的元素,以表示没有节点连接到上一级的上级节点。</p>
<pre>[1,2,3,4,5,6,null]
[null,null,7,8,9,10,null]
[null,11,12,null]
</pre>
<p>合并所有序列化结果,并去除末尾的 null 。</p>
<pre>[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>节点数目不超过 1000</li>
<li><code>1 &lt;= Node.val &lt;= 10^5</code></li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>链表</li></div></div>\n<div><li>👍 199</li><li>👎 0</li></div>