My iterative Java solution


  • 1
    B
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode merged = new ListNode(0);
        ListNode start = merged;
        while (l1!=null || l2!= null){
            if (l1 == null){
                start.next = l2;
                return merged.next;
            }
            if (l2 == null){
                start.next = l1;
                return merged.next;
            }
            if (l1.val < l2.val){
                start.next = l1;
                l1 = l1.next;
                start = start.next;
            }
            else {
                start.next = l2;
                l2 = l2.next;
                start = start.next;    
            }
        }
        return merged.next;
    }

Log in to reply
 

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