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;
}
}
}
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;
}
}
}
No comments:
Post a Comment