該算法更爲巧妙,不用保存原始鏈表的映射關係,構建新節點時,指針作以下變化,即把新節點插入到相應的舊節點後面:算法
一、構建新節點random指針:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->nextdom
二、恢復原始鏈表以及構建新鏈表:例如old1->next = old1->next->next, new1->next = new1->next->nextthis
本人額原算法超時:
public class CopyListRandom { private static class RandomListNode{ int lable; RandomListNode next=null,random=null; RandomListNode(int x){ this.lable=x; } } public RandomListNode copyRandomList(RandomListNode head){ if(head==null) return null; RandomListNode p=head; RandomListNode q=deepCopy(p),r=q; System.out.println("p"+p.lable); p=head; System.out.println("ph:"+p.lable); while(p!=null){ RandomListNode tmp=head,s=q; while(p.random!=tmp){ tmp=tmp.next; s=s.next; } r.random=s; r=r.next; p=p.next; } return q; } public RandomListNode deepCopy(RandomListNode p){ RandomListNode another=null; if(p!=null){ another=new RandomListNode(p.lable); another.next=deepCopy(p.next); } return another; } public static void main(String[] args){ RandomListNode head=new RandomListNode(1); RandomListNode head1=new RandomListNode(2); RandomListNode head2=new RandomListNode(3); RandomListNode head3=new RandomListNode(4); RandomListNode head4=new RandomListNode(5); RandomListNode head5=new RandomListNode(6); head.next=head1; head1.next=head2; head2.next=head3; head3.next=head4; head4.next=head5; head5.next=null; head.random=head2; head1.random=head3; head2.random=head4; head3.random=head5; head4.random=head; head5.random=head1; RandomListNode tmp=head; while(tmp!=null){ System.out.println(tmp+" "+tmp.lable+" random:"+tmp.random.lable); tmp=tmp.next; } CopyListRandom copy=new CopyListRandom(); RandomListNode tmp2=copy.copyRandomList(head); while(tmp2!=null){ System.out.println(tmp2+" "+tmp2.lable+" random:"+tmp2.random.lable); tmp2=tmp2.next; } } }
上述方法的代碼私下:
package com.demo.acm; public class CopyListRandom { private static class RandomListNode{ int label; RandomListNode next=null,random=null; RandomListNode(int x){ this.label=x; } } public RandomListNode copyRandomList(RandomListNode head){ if(head==null) return null; RandomListNode p=head,q=head; p=deepCopy(p);//指針複製 //兩個鏈表合併 while(q!=null){ RandomListNode tmp1=q.next,tmp2=p.next; q.next=p; p.next=tmp1; p=tmp2; q=tmp1; } //將合併鏈表的隨機指針整合 q=head; p=head.next; while(q!=null){ if(q.random!=null){ p.random=q.random.next; }else{ p.random=null; } if(q.next.next!=null){ q=q.next.next; }else{ break; } if(p.next!=null){ p=p.next.next; }else{ break; } } //分拆成兩個鏈表 q=head; p=head.next; RandomListNode tmp=p; while(q!=null){ if(q.next.next!=null){ q.next=q.next.next; q=q.next; }else{ q.next=null; q=q.next; } if(p.next!=null){ p.next=p.next.next; p=p.next; } else{ p.next=null; p=p.next; } } return tmp; } public RandomListNode deepCopy(RandomListNode p){ RandomListNode another=null; if(p!=null){ another=new RandomListNode(p.label); another.next=deepCopy(p.next); } return another; } public static void main(String[] args){ RandomListNode head=new RandomListNode(1); RandomListNode head1=new RandomListNode(2); RandomListNode head2=new RandomListNode(3); RandomListNode head3=new RandomListNode(4); head.next=head1; head1.next=head2; head1.random=head2; head2.next=head3; head3.next=null; CopyListRandom copy=new CopyListRandom(); RandomListNode tmp=copy.copyRandomList(head); while(tmp!=null){ System.out.println(tmp.label); if(tmp.random!=null) System.out.println("random:"+tmp.random.label); tmp=tmp.next; } } }