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*/
}