My java solution with 260ms


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

Log in to reply
 

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