138:复制带随机指针的链表

This commit is contained in:
huangge1199 2021-06-07 10:34:15 +08:00
parent e8deaa3f17
commit 0009870553
2 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,121 @@
//给你一个长度为 n 的链表每个节点包含一个额外增加的随机指针 random 该指针可以指向链表中的任何节点或空节点
//
// 构造这个链表的 深拷贝 深拷贝应该正好由 n 全新 节点组成其中每个新节点的值都设为其对应的原节点的值新节点的 next 指针和 random
//指针也都应指向复制链表中的新节点并使原链表和复制链表中的这些指针能够表示相同的链表状态复制链表中的指针都不应指向原链表中的节点
//
// 例如如果原链表中有 X Y 两个节点其中 X.random --> Y 那么在复制链表中对应的两个节点 x y 同样有 x.random
//--> y
//
// 返回复制链表的头节点
//
// 用一个由 n 个节点组成的链表来表示输入/输出中的链表每个节点用一个 [val, random_index] 表示
//
//
// val一个表示 Node.val 的整数
// random_index随机指针指向的节点索引范围从 0 n-1如果不指向任何节点则为 null
//
//
// 你的代码 接受原链表的头节点 head 作为传入参数
//
//
//
// 示例 1
//
//
//
//
//输入head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
//输出[[7,null],[13,0],[11,4],[10,2],[1,0]]
//
//
// 示例 2
//
//
//
//
//输入head = [[1,1],[2,1]]
//输出[[1,1],[2,1]]
//
//
// 示例 3
//
//
//
//
//输入head = [[3,null],[3,0],[3,null]]
//输出[[3,null],[3,0],[3,null]]
//
//
// 示例 4
//
//
//输入head = []
//输出[]
//解释给定的链表为空空指针因此返回 null
//
//
//
//
// 提示
//
//
// 0 <= n <= 1000
// -10000 <= Node.val <= 10000
// Node.random 为空null或指向链表中的节点
//
// Related Topics 哈希表 链表
// 👍 575 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.Node;
import java.util.HashMap;
import java.util.Map;
//138:复制带随机指针的链表
public class CopyListWithRandomPointer{
public static void main(String[] args) {
//测试代码
Solution solution = new CopyListWithRandomPointer().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
Map<Node, Node> map = new HashMap<>();
return copy(head, map);
}
private Node copy(Node head, Map<Node, Node> map) {
if (head == null) {
return null;
}
if (map.containsKey(head)) {
return map.get(head);
}
Node node = new Node(head.val);
map.put(head, node);
node.next = copy(head.next, map);
node.random = copy(head.random, map);
return node;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,64 @@
<p>给你一个长度为 <code>n</code> 的链表,每个节点包含一个额外增加的随机指针 <code>random</code> ,该指针可以指向链表中的任何节点或空节点。</p>
<p>构造这个链表的 <strong><a href="https://baike.baidu.com/item/深拷贝/22785317?fr=aladdin" target="_blank">深拷贝</a></strong>。 深拷贝应该正好由 <code>n</code><strong>全新</strong> 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 <code>next</code> 指针和 <code>random</code> 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。<strong>复制链表中的指针都不应指向原链表中的节点 </strong></p>
<p>例如,如果原链表中有 <code>X</code><code>Y</code> 两个节点,其中 <code>X.random --> Y</code> 。那么在复制链表中对应的两个节点 <code>x</code><code>y</code> ,同样有 <code>x.random --> y</code></p>
<p>返回复制链表的头节点。</p>
<p>用一个由 <code>n</code> 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 <code>[val, random_index]</code> 表示:</p>
<ul>
<li><code>val</code>:一个表示 <code>Node.val</code> 的整数。</li>
<li><code>random_index</code>:随机指针指向的节点索引(范围从 <code>0</code> 到 <code>n-1</code>);如果不指向任何节点,则为  <code>null</code> 。</li>
</ul>
<p>你的代码 <strong></strong> 接受原链表的头节点 <code>head</code> 作为传入参数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e1.png" style="height: 138px; width: 680px;" /></p>
<pre>
<strong>输入:</strong>head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
<strong>输出:</strong>[[7,null],[13,0],[11,4],[10,2],[1,0]]
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e2.png" style="height: 111px; width: 680px;" /></p>
<pre>
<strong>输入:</strong>head = [[1,1],[2,1]]
<strong>输出:</strong>[[1,1],[2,1]]
</pre>
<p><strong>示例 3</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/09/e3.png" style="height: 119px; width: 680px;" /></strong></p>
<pre>
<strong>输入:</strong>head = [[3,null],[3,0],[3,null]]
<strong>输出:</strong>[[3,null],[3,0],[3,null]]
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>head = []
<strong>输出:</strong>[]
<strong>解释:</strong>给定的链表为空(空指针),因此返回 null。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= n <= 1000</code></li>
<li><code>-10000 <= Node.val <= 10000</code></li>
<li><code>Node.random</code> 为空null或指向链表中的节点。</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>链表</li></div></div>\n<div><li>👍 575</li><li>👎 0</li></div>