1ms java solution


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

Log in to reply
 

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