其他分享
首页 > 其他分享> > 关于链表

关于链表

作者:互联网

 


 

/*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
import java.util.*;
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        if(pHead==null)
            return null;
        
        RandomListNode Lphead=pHead;
        Map<RandomListNode,RandomListNode> map=new HashMap<>();
        while(Lphead!=null){
            map.put(Lphead,new RandomListNode(Lphead.label) ) ;
            Lphead=Lphead.next;
        }
        Lphead=pHead;
        while(Lphead!=null){
            map.get(Lphead).next=map.get(Lphead.next);
            Lphead=Lphead.next;
        }
        Lphead=pHead;
        while(Lphead!=null){
            map.get(Lphead).random=map.get(Lphead.random);
            Lphead=Lphead.next;
        }
        
        return map.get(pHead);
    }
}

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode p1,ListNode p2) {
        if(p1==null)
            return p2;
        if(p2==null)
            return p1;
        
        ListNode head=new ListNode(-1);
        ListNode L=head;
        while(p1!=null && p2!=null){
            if(p1.val<p2.val){
                L.next=p1;
                p1=p1.next;
                L=L.next;
            }else{
                L.next=p2;
                p2=p2.next;
                L=L.next;
            }
        }
        
        while(p1!=null){
                L.next=p1;
                p1=p1.next;
                L=L.next;
        }
        while(p2!=null){
                L.next=p2;
                p2=p2.next;
                L=L.next;
        }
        return head.next;
        
    }
}

 

标签:map,RandomListNode,ListNode,next,链表,关于,null,Lphead
来源: https://www.cnblogs.com/lemonzhang/p/13092291.html