How will u find whether a linked list has a loop or not?
NOTE:[No extra memory is to be used. The loop may intersect even at center]
Subscribe to:
Post Comments (Atom)
This blog is exclusively for GCT CSITA students. Contact us at 'gct.csita@gmail.com'.
4 comments:
waste of time
it should hav two pointers ,move one opinter twice as de speed of de other pointer..if the pointers coincide then there is loop will exist.otherwise no loop
following code will show the massage if loop exits
for(cur=first,temp=cur->next;cur;cur=cur->next)
{
for(int i=0;i<2;temp=temp->next)
if(cur=temp)
{
printf("THERE IS LOOP!!")
break;
}
}
have 2 pointers..move second twice the speed of first..if they coincide,loop exists.
you can reverse list.you ll meet with HEAD again if ter s a loop..
both logic involves no extra memory..
Post a Comment