刷刷题。。提升一下逻辑能力。。
这道题,主要就是如何复制这个random对应的那个节点
我的思路就是建立一个hashmap ,把新list和老list的每个节点对应起来,然后根据查表来获取random对的新队列的节点
public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if(head==null){ return null; } RandomListNode newhead=null; RandomListNode temp=null; boolean mark=true; HashMap<RandomListNode,RandomListNode> table=new HashMap<>(); while(head!=null){ if(mark) { newhead = new RandomListNode(head.label); temp = newhead; table.put(head, temp); mark = false; head = head.next; continue; } temp.next=new RandomListNode(head.label); temp=temp.next; table.put(head,temp); head = head.next; } for(Map.Entry<RandomListNode,RandomListNode>entry:table.entrySet()){ entry.getValue().random=table.get(entry.getKey().random); } return newhead; } }