关于链表

关于链表

 


 

/*
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;
        
    }
}

 

上一篇:剑指offer_复杂链表的复制


下一篇:剑指offer-复杂链表的复制