Intersection point of two linked lists

CODE: https://ide.codingblocks.com/s/650804

In the above code I am following the below approach:

  1. I made a floyd’s cycle. Let suppose we have two linked lists :

I traverse the linked list A till NULL and point the next pointer to the list B.

  1. Now we can apply floyd’s cycle algorithm in it.

  2. For linked lists which do not intersect have no common point of intersection and also do not form cycle.

PS: Can you please tell me where is my code or approach is wrong.

I have also implemented with one another approach given in HINTS but it is also throwing the error.

CODE: https://ide.codingblocks.com/s/650808

hello @rahul_bansal,
Your condition is wrong, it should be

while(fast!=NULL && fast->next!=NULL)

Still the code is Not Working

Hello @rahul_bansal,
There are two errors in your code ( CODE: https://ide.codingblocks.com/s/650808),

  1. You didn’t swap d1 and d2 if(d1<d2)

swap(d1,d2)

  1. You are not moving l2 pointer,

while(l1!=NULL){

 if(l1==l2)
    return l1;
 l1 = l1->next;
 l2 = l2->next;

}

Also after making these changes could you please submit it, you are only compiling it.

Ya Thanks! Now the code is working fine but can you please also correct me in my first approach using floyd’s cycle algorithm.

CODE: https://ide.codingblocks.com/s/650804

1 Like

Hello @rahul_bansal,

You did’nt change this line which iI told you earlier:

while(fast!=NULL && fast->next!=NULL)

Again, after making this change please try submitting it.

Now it is working after submitting. Actually I am only compiling the code. But fine Thanks!

1 Like

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.