Detect if the given linked list cyclic or acyclic.bool IsCyclic(Node *n)
{
if (nullptr == n)
return false;
Node *slow = n;
Node *fast = n;
while (nullptr != fast->next && nullptr != fast->next->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
} |