share my java solution


  • 0
    W
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null) {
            return l1 == null? l2 : l1;
        }
        ListNode start = new ListNode(0);
        ListNode tmp = start;
        while(l1 != null && l2 != null) {
            tmp.next = l1.val < l2.val ? l1 : l2;
            if(l1.val < l2.val) l1 = l1.next;
            else l2 = l2.next;
            tmp = tmp.next;
        }
        tmp.next = l1 == null ? l2 : l1;
        return start.next;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.