Simple 1 ms Java solution


  • 0
    N
    ListNode getSmaller(ListNode l1, ListNode l2){
    	if (l1 == null)
    		return l2;
    	if (l2 == null)
    		return l1;
    	return (l1.val < l2.val ? l1 : l2);
    }
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2){
    	if (l1 == null && l2 == null)
    		return null;
    	ListNode head = getSmaller(l1, l2);
    	ListNode current = head;
    	while (l1 != null || l2 !=null){
    		if (current == l1)
    			l1 = l1.next;
    		if (current == l2)
    			l2 = l2.next;
    		ListNode temp = getSmaller(l1, l2);
    		current.next = temp;
    		current = temp;
    	}
    	return head;
    }

Log in to reply
 

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