解题思路:
深搜提交代码:维护map
class Solution {
Map<RandomListNode,RandomListNode> map=new HashMap<>();
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null) return null;
RandomListNode newHead=new RandomListNode(head.label);
map.put(head, newHead);
if(head.random!=null) {
if(map.containsKey(head.random))
newHead.random=map.get(head.random);
else {
newHead.random=copyRandomList(head.random);
}
}else
newHead.random=null;
if(head.next!=null) {
if(map.containsKey(head.next))
newHead.next=map.get(head.next);
else
newHead.next=copyRandomList(head.next);
}else
newHead.next=null;
return newHead;
}
}
运行结果: