@ above

your code is only detecting loop.. my code is detecting loop and then
removing loop as well

On Thu, Jan 13, 2011 at 5:04 PM, bittu <[email protected]> wrote:

> @snehal ..although ur approach ir good but u make d problem little
> complex,also missed out little checking, it can be done by using 2
> pointers  & single while loop--Now Instruction(CPU) Matters.
>
> Rather then presenting different-2 algorithm i will present very
> famous algorithm Floyd’s Cycle-Finding Algorithm:
>
> it is the fastest method. Traverse linked list using two pointers.
> Move one pointer by one and other pointer by two.  If these pointers
> meet at some node then there is a loop.  If pointers do not meet then
> linked list doesn’t have loop.
>
> Time complexity O(n)
> Space Complexity O(1)
>
>
> Code-Snippet  Below
>
> int is_loop(struct node *list)
> {
>  struct node  *slow_p = list, *fast_p = list;
>
>  while(slow_p && fast_p &&
>          slow_p->next &&
>          fast_p->next &&
>          fast_p->next->next )
>  {
>    slow_p = slow_p->next;
>    fast_p  = fast_p->next->next;
>    if (slow_p == fast_p)
>    {
>       printf("Found Loop");
>       return 1;
>    }
>  }
>  return 0;
> }
>
>
> It Will Surely Works.
>
>
> Regards
> Shashank Mani "Don't b Evil U Can Learn whilw U Earn"
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to