1ms java solution easy to understand


  • 0
    N
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode head=null,other1=null,other2=null;
        if(l1.val<=l2.val)
        {
            head=l1;
            other2=l2; 
        }else{
            head=l2;
            other2=l1;
        }
        other1=head;
        while(other1.next!=null)
        {
            ListNode temp=null;  
            if(other1.next.val>other2.val)
            {
                temp=other1.next;
                other1.next=other2;
                other2=temp;
            }
            other1=other1.next;    
    
        }
        
        if(other1.next==null) other1.next=other2;
        return head;
        
        
    }

Log in to reply
 

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