Merge Two Sorted Lists

很简单

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode h = new ListNode(-1);
        ListNode l3 = h;
        while(l1!=null && l2!=null){
            if(l1.val < l2.val){
                l3.next = l1;
                l3 = l3.next;
                l1 = l1.next;
            }else{
                l3.next = l2;
                l3 = l3.next;
                l2 = l2.next;
            }
        }
        
        if(l1==null && l2!=null) l3.next = l2;
        if(l2==null && l1!=null) l3.next = l1;
        
        return h.next;
    }
}

 

posted @ 2015-04-07 03:43  世界到处都是小星星  阅读(99)  评论(0编辑  收藏  举报