剑指 Offer 25. 合并两个排序的链表

 

剑指 Offer 25. 合并两个排序的链表

 

遍历

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0),curr= head;
        while(l1 != null && l2 != null)
        {
            if(l1.val < l2.val)
            {
                curr.next = l1;
                l1 = l1.next; 
            }
            else
            {
                curr.next = l2;
                l2 = l2.next;
            }
            curr = curr.next;
        }
        if(l1!=null)
        {
            curr.next = l1;
        }
        else 
        {
            curr.next = l2;
        }
        return head.next;
    }
}

 

 

 

上一篇:Java——集合


下一篇:力扣 - 剑指 Offer 25. 合并两个排序的链表