My Java Solution


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

Log in to reply
 

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