给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
要求返回这个链表的 深拷贝。
我们用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 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/copy-list-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:一个数组,一个hashmap 散列表。
数组用来保存新链表的每一个节点,hashmap用来保存原始节点和对应的坐标
1.先遍历原始链表,复制每一个节点保存到数组中,并将每一个节点对应的指针和坐标保存到hashmap之中。
2、同步遍历原始链表和新节点的数组,假设当前遍历原始链表的节点为p,遍历数组对应的节点为q,读取每一个原始节点p的random指针,使用hashmap来定位这个指针的坐标,并通过该坐标在数组中找到对应的节点t,将当前节点q的random指针指向节点t。
执行用时:1 ms, 在所有 Java 提交中击败了16.94%的用户
内存消耗:38 MB, 在所有 Java 提交中击败了70.40%的用户
/*
// 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 head;
ArrayList<Node> newnodelist = new ArrayList<>();
HashMap<Node,Integer> nodemap = new HashMap<>();
Node p = head,newhead=null;
int i=1;
nodemap.put(null, 0);
newnodelist.add(null);
while(p!=null){
nodemap.put(p,i);
newnodelist.add(new Node(p.val));
if(i>1){
newnodelist.get(i-1).next=newnodelist.get(i);
}
i++;
p=p.next;
}
//获得原链表 节点对应的坐标的映射,以及用数组存储新链表的节点并前后连接
i=1;
p=head;
while(p!=null){
//遍历原链表,获得每一个节点的随机指针对应的坐标所对应的新节点
//将当前的新节点连接到 对应的随机节点
newnodelist.get(i++).random = newnodelist.get(nodemap.get(p.random));
p=p.next;
}
if(newnodelist.get(1)!=null){
newhead = newnodelist.get(1);
}
return newhead;
}
}
思路2:
用一个hashmap,原始节点对应新节点。
解答成功:
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:38.1 MB,击败了62.56% 的Java用户
class Solution {
public Node copyRandomList(Node head) {
if(head==null) return head;
HashMap<Node,Node> nodemap = new HashMap<>();
Node p = head,q=null,t=null;
//t用来保存每一个新节点前一个节点,用来前后连接节点
//p用来遍历原节点,q用生成对应新节点
while(p!=null){
q=new Node(p.val);//生成新节点
if(t!=null) t.next=q;//连接新节点前后的节点
nodemap.put(p, q);//原节点对应新节点
t=q;//t来保存前一个位置的节点
p=p.next;
}
p=head;
//再次遍历原链表,使得每一个原节点对应的新节点,分配对应的随机节点
while(p!=null){
nodemap.get(p).random=nodemap.get(p.random);
p=p.next;
}
return nodemap.get(head);
}
}
思路3:一个记录该节点是否已经被创建的hash表。递归方法,沿着next,random,进行深度递归。
如果当前元素创没有创建则存到一个记录节点的hash表中,否则直接返回
解答成功:
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:38.2 MB,击败了51.00% 的Java用户
class Solution {
HashMap<Node,Node> nodemap = new HashMap<>();
public Node copyRandomList(Node head) {
//检查空
if(head==null) return head;
//检查是否已经出现过
if(nodemap.containsKey(head)) return nodemap.get(head);
//创建新节点
nodemap.put(head, new Node(head.val));
//递归 复制创建/连接 head 的 next和random
nodemap.get(head).next = copyRandomList(head.next);
nodemap.get(head).random = copyRandomList(head.random);
//始终返回当前复制的节点
return nodemap.get(head);
}
}