cycle detection in list

int HasCycle(Node* head)
    {
       // Complete this function
       // Do not write the main method
        Node * fast = head;
        Node * slow = head;
        while(fast!=NULL && slow!=NULL && fast->next)
            {

            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow)
            {
            return 1;
            }
        }
        return 0;
    }

Comments

Popular posts from this blog

How whatsapp works

Android N Developer Preview? It's exciting for most of you