1 题目
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
示例 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。
提示:
-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2 Java
2.1 !方法一(巧妙)
链表的节点间关系处理(比如插入),都是通过while循环(判断节点是否为null),循环内先重建关系,再挪动节点至下一位置
/*
// 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) {
if(head == null) return null;
// 在原链表每个空隙插入新节点,对新节点val赋值
Node node = head;
while(node != null){
Node newNode = new Node(node.val);
newNode.next = node.next;
node.next = newNode;
node = node.next.next;
}
// 对新节点的random关系赋值
node = head;
while(node != null){
node.next.random = (node.random != null) ? node.random.next : null;
node = node.next.next;
}
// 将新旧节点之间next关系拆开
node = head;
Node newNode = node.next;
Node newHead = head.next;
while(node != null){
node.next = node.next.next;
newNode.next = (newNode.next != null) ? newNode.next.next : null;
node = node.next;
newNode = newNode.next;
}
return newHead;
}
}
2.2 !方法二(递归)
递归方法得到的是根据参数head复制出的node,将生成的node记录在HashMap中,这样可以避免第二次寻找时(random)再次生成新的节点
/*
// 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 {
HashMap<Node, Node> copyNodeMap = new HashMap<Node, Node>();
public Node copyRandomList(Node head) {
if(head == null) return null;
if(copyNodeMap.containsKey(head)) return copyNodeMap.get(head);
Node node = new Node(head.val);
copyNodeMap.put(head, node); // 创建完该节点立刻记录
node.next = copyRandomList(head.next);
node.random = copyRandomList(head.random);
return node;
}
}
3 二刷
×3.1 方法一(HashMap记录duplicate)
这个写法不太好,不用看了
/*
// 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) {
// 原链表
Node cur = head;
// 复制链表
Node headNew = new Node(0);
Node curNew = headNew;
// <原链表节点, 复制链表节点>
Map<Node, Node> map = new HashMap<>();
// 遍历原链表
while(cur != null){
// 处理 next 节点
// 用 cur 复制出一个新节点,连接到curNew后面
if(!map.containsKey(cur)){
curNew.next = new Node(cur.val);
map.put(cur, curNew.next);
}else{
curNew.next = map.get(cur);
}
// curNew 挪到新节点,至此,curNew 是 cur 对应的副本
curNew = curNew.next;
// 处理 random 节点
// 再复制 random
if(cur.random != null){
if(!map.containsKey(cur.random)){
curNew.random = new Node(cur.random.val);
map.put(cur.random, curNew.random);
}else{
curNew.random = map.get(cur.random);
}
}
// 本节点处理完毕,处理下一个节点
cur = cur.next;
}
return headNew.next;
}
}
!3.2 方法二(HashMap记录duplicate;推荐)
/*
// 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) {
// 原链表
Node cur = head;
// <原链表节点, 复制链表节点>
Map<Node, Node> map = new HashMap<>();
// 复制每一个节点;但新节点之间没有任何连接关系
while(cur != null){
map.put(cur, new Node(cur.val));
cur = cur.next;
}
// 处理新节点之间连接关系
cur = head;
while(cur != null){
// 当前节点 对应duplicate 的next/random = 当前节点 next/random 对应的duplicate
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(head);
}
}
3.3 方法三(递归)
/*
// 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 {
// 记录所有的 节点 及其对应 duplicate
Map<Node, Node> map = new HashMap<>();
// 传入 head,返回对应 duplicate
public Node copyRandomList(Node head) {
if(head == null) return null;
// head 对应的 duplicate 已存在,直接返回
if(map.containsKey(head)) return map.get(head);
// 先复制 head,生成对应 duplicate
Node duplicate = new Node(head.val);
map.put(head, duplicate);
// 再处理 duplicate 的连接关系
duplicate.next = copyRandomList(head.next);
duplicate.random = copyRandomList(head.random);
return duplicate;
}
}