这道题确实有难度,我想了很久都没想到如何实现复制链表的随机指针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 {
public Node copyRandomList(Node head) {
if(head==null){
return null;
}
Node p=head;
//将链表中的节点复制一份,但Random的关系仍然没有发生改变
while(p!=null){
Node copyNode=new Node(p.val);
copyNode.next=p.next;
p.next=copyNode;
p=p.next.next;
}
//复制随机链表关系
p=head;
while(p!=null){
if(p.random!=null){
p.next.random=p.random.next;
}
p=p.next.next;
}
//链表分离
p=head;
Node tempP=head.next;
Node newHead=head.next;//返回值:新链表
while(p!=null){
p.next=tempP.next;
p=p.next;
if(tempP.next!=null){
tempP.next=tempP.next.next;
tempP=tempP.next;
}
}
return newHead;
}
}
二、常规哈希表解法
通过构建原有链表与新建链表之间的映射关系来完成随机链表的关系复制。
/*
// 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;
}
Node p=head;
Map<Node,Node> map=new HashMap<>();
while(p!=null){
map.put(p,new Node(p.val));
p=p.next;
}
p=head;
while(p!=null){
map.get(p).next=map.get(p.next);
map.get(p).random=map.get(p.random);
p=p.next;
}
return map.get(head);
}
}