leetcode copy List with Random

该算法更为巧妙,不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:

1、构建新节点random指针:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->next

2、恢复原始链表以及构建新链表:例如old1->next = old1->next->next,  new1->next = new1->next->next


本人额原算法超时:
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;
        }
    }
}


 
原文地址:https://www.cnblogs.com/csxf/p/3644005.html