1.先合链 2.再分链
public class Solution {
public RandomListNode Clone(RandomListNode pHead) {
if(pHead==null)
{
return null;
}
RandomListNode cur=pHead;
while(cur!=null)
{
RandomListNode clone=new RandomListNode(cur.label);
clone.next=cur.next;
cur.next=clone;
cur=clone.next;
}
cur=pHead;
while(cur!=null)
{
RandomListNode clone=cur.next;
if(cur.random!=null)
{
clone.random=cur.random.next;
}
cur=clone.next;
}
cur=pHead;
//分链
RandomListNode clonePhead=pHead.next;
while(cur.next!=null)
{
RandomListNode nex=cur.next;
cur.next=nex.next;
cur=nex;
}
return clonePhead;
}
}