Showing posts with label cyclic. Show all posts
Showing posts with label cyclic. Show all posts

Tuesday, December 8, 2009

Determine whether given LinkedList is Cyclic or Acyclic

Determine whether given LinkedList is Cyclic or Acyclic

bool determineTermination( Node *head )
{
    Node *fast, *slow;
    fast = slow = head;
    while( true )
    {
        if( !fast || !fast->next )
            return false;
        else if( fast == slow || fast->next == slow )
            return true;
        else
        {
            slow = slow->next;
            fast = fast->next->next;
        }
    }
}