I m getting a run error in this code. Help me resolve it please.
Intersection of two linked lists problem
Hey @apoorvas1301
Mentioned the changes in comments
Node *intersectionOfTwoLinkedLists(Node *l1, Node *l2)
{
if(l1==NULL or l2==NULL)
{
return NULL;
}
Node* temp1=l1;
Node* temp2=l2;
int len1=0,len2=0,lendiff;
while(temp1!=NULL)
{
temp1=temp1->next;
len1++;
}
while(temp2!=NULL)
{
temp2=temp2->next;
len2++;
}
lendiff=abs(len1-len2);
temp1=l1; //reinitialize because point to end now
temp2=l2; //reinitialize because point to end now
while(lendiff--)
{
if(len1>len2)
{
temp1=temp1->next;
}
else
{
temp2=temp2->next;
}
}
while(temp1!=NULL && temp2 !=NULL &&temp1!=temp2) //checking if not null and also we have to match nodes
{
temp1=temp1->next;
temp2=temp2->next;
// return temp1; //this shoulf be outside
}
return temp1;//took that outside
return NULL;
}
Also I think there is some technical issue as its giving MLE on already accepted solution as well ,so try tomorrow ,if it still gives problem then let me know 
yes, it did work. Thanks
1 Like