实现复制另一个链表,用的就是hashmap
让这个链表和我们复制之后的链表,分别作为key和value
import java.util.*;
public class Solution {
HashMap<RandomListNode,RandomListNode> map = new HashMap<>();
public RandomListNode Clone(RandomListNode pHead)
{
if(pHead==null){
return null;
}
if(map.containsKey(pHead)){
return map.get(pHead);
}
RandomListNode temp = new RandomListNode(pHead.label);
map.put(pHead,temp);
temp.next = Clone(pHead.next);
temp.random = Clone(pHead.random);
return temp;
}
}