Intersection of linked list


why its getting wrong ??
can’t we make any function outside given function to find out the length of list

Ashish, you need to slightly change the function in your code as :

int intersection(nodehead1,nodehead2,int N1,int N2)
{
int len1=N1;
int len2=N2;
int diff;
if(len2>len1)
{
diff=len2-len1;
}
else
{
diff=len1-len2;
}
if(len2>len1)
{
for(int i=0;i<diff;i++)
{
head2=head2->next;
}
}
else
{
for(int i=0;i<diff;i++)
{
head1=head1->next;
}
}
while(head1 != NULL && head2 != NULL)
{
if(head1->data != head2->data)
{
head1 = head1->next;
head2 = head2->next;
}
else if(head1->data==head2->data)
{
return head1->data;
}
}
return -1;
}

ok i got this one but i want to know can we make func for length outside intersection() function given in the solutio already or we have to seperatly cal L1 and L2 inside this given function

Yes, you can make a separate function for length outside intersection function and can call them inside the intersection function.

done thank you so much, can you also plz tell me how to clone a linked list with random pointers ??

i know doubly linked list but actually idk whats are random pointers

I didn’t got wht exactly you are trying to ask…can u give some examples ?

lets say we have a doubly linked list with one ptr points to next node and another ptr point any node of linked list and we have to make duplicate of this linked list

I guess in doubly linked list, you are only required to have the prev and next pointers… There are no concept of random pointers …