Time limit is exceeding; please suggest changes in my code

int length(Node* head)

{
int cnt=0;
while(head!=NULL)
{
cnt++;
head=head->next;
}
return cnt;
}

Node *intersectionOfTwoLinkedLists(Node *l1, Node *l2)
{

int len1=length(l1);
int len2=length(l2);


if(len1>len2)
{
    int k=len1-len2;

    while(k!=0)
    {
        if(l1==NULL)
        return NULL;
        l1=l1->next;
        k--;
    }
}

else{
    int k=len2-len1;

    while(k!=0)
    { if(l2==NULL)
        return NULL;
        l2=l2->next;
        k--;
    }
}

while(l1!=NULL and l2!=NULL)
{
    if(l1==l2)
    return l1;
    l1=l1->next;
    l2=l2->next;

}

return l1;
/*Code here*/

}

hi @anandsingh3210
ur code is absolutely fine… just click on submit button it will pass all test cases…

yes it is submitted but when I was performing compile and test it was showing time limit exceed. So please inform cb about this.

Sure i will pass on this to the team…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.