Loading

21. [链表]合并两个有序链表

21. 合并两个有序链表

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode sentinel = new ListNode(-1);
        ListNode curr = sentinel;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                sentinel.next = l1;
                l1 = l1.next;
            } else {
                sentinel.next = l2;
                l2 = l2.next;
            }
            sentinel = sentinel.next;
        }
        sentinel.next = l1 != null ? l1 : l2;
        return curr.next;
    }
}
posted @ 2020-10-24 10:52  上海井盖王  阅读(93)  评论(0)    收藏  举报