Monday, July 28, 2008

cpuzzle3

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]

4 comments:

Unknown said...

waste of time

Unknown said...

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

Unknown said...

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;
}
}

vivek said...

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..