Time limit exceed - java - please help


  • 0
    N

    public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode head=new ListNode(0);
    ListNode node=head;
    if(l1==null&&l2==null)
    return null;
    else if(l2==null)
    {
    return l1;
    }
    else if(l1==null)
    {
    return l2;
    }

        while(l1!=null&&l2!=null)
    {
        if(l1.val<l2.val)
        {
        node.next=l1;
        node=node.next;
        l1=l1.next;
        }else if( l1.val==l2.val)
        {
            node.next=l1;
            node=node.next;
            node.next=l2;
            node=node.next;
            l1=l1.next;
            l2=l2.next;
        }else
        {
            node.next=l2;
            node=node.next;
            l2=l2.next;
        }
           
    }
    if(l1!=null)
    {
        while(l1!=null)
        {
            node.next=l1;
            node=node.next;
            l1=l1.next;
        }
    }
    if(l2!=null)
    {
        while(l2!=null)
        {
            node.next=l2;
            node=node.next;
            l2=l2.next;
        }
    }
    head=head.next;
    return head;
    

    }
    }


Log in to reply
 

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