A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Subscribe to see which companies asked this question.
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
/*
自己的方案:
首先第一次遍历,复制每个node,构成了个基本的单链表
第二次遍历:对每个node的随机后继, 使用for循环查出 它与头结点的距离,再根据这个距离在找复制的单链表上找到随机后继,指向它。
时间复杂度O(n^2)
严格来说会超时
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null;
RandomListNode tmp = head;
RandomListNode copyListhead = new RandomListNode(tmp.label);
copyListhead.next = null;
copyListhead.random = tmp.random == null? null:copyListhead;
RandomListNode tmp1 = copyListhead;
while (tmp.next != null)
{
tmp = tmp.next;
tmp1.next = new RandomListNode(tmp.label);
tmp1 = tmp1.next;
}
tmp1.next = null;
tmp = head;
tmp1 = copyListhead;
RandomListNode start,randomNode;
start = head;
// 用于计算每个node的随机后继,距离头结点的距离, start在计算距离时初始化为head,在根据距离再copy上后移时初始化copyListhead
int length=0;
while (tmp!= null)
{
randomNode = tmp.random;
if (randomNode == null)
tmp1.random = null;
else
{
for (length = 0; start != randomNode; start = start.next, length++) ;
//计算head到randomNode的距离
start = copyListhead;
for (int i = 0; i < length; start = start.next, i++) ;
//根据距离到达randomNode的copy点
tmp1.random = start;
start = head;
}
tmp = tmp.next;
tmp1 = tmp1.next;
}
return copyListhead; }
}
//问题的关键:时间主要浪费在查找randomNode上,所以优化这个步骤是关键。
//这里的解决方案,使用了哈希表。
//Map中存的key为原node,value为node副本
//从而使得能够在O(N)完成
//哈希表的妙用:快速的定位资源
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
Map<RandomListNode,RandomListNode> map = new HashMap<>();
RandomListNode tmp = head,copy;
while (tmp!=null)
{
map.put(tmp,new RandomListNode(tmp.label));
tmp = tmp.next;
}
tmp = head;
while (tmp!=null)
{
map.get(tmp).next = map.get(tmp.next);
map.get(tmp).random = map.get(tmp.random);
tmp = tmp.next;
}
return map.get(head);
}
}