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

在这里插入图片描述
简单题,就不多写了

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

你可能感兴趣的:(力扣)