Clean,Crisp and fast iterative solution


  • 2
    B

    ,,,

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

    ,,,


Log in to reply
 

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