Concise Java Accepted Solution,just a few lines,254ms


  • 1
    J
    public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode l3=new ListNode(0);
        ListNode l4=l3;
        while(l1!=null || l2!=null){
            l3.next=(l1!=null && l2==null)||(l1!=null && l2!=null && l1.val<l2.val)  ?  l1:l2;
            if( (l1!=null && l2==null)||(l1!=null && l2!=null && l1.val<l2.val) )    l1=l1.next;
            else   l2=l2.next;
            l3=l3.next;
        }
        return l4.next;
    }
    

    }
    The idea is simple: just select the right node from each List.
    The logic is here: l3.next=(l1!=null && l2==null)||(l1!=null && l2!=null && l1.val<l2.val) ? l1:l2;


Log in to reply
 

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