很简单的一种方法就是,设置两个指针,一个每次递增一步,一个每次递增两步,若是有环二者必然重合。
struct node
{
char val;
node *next;
};
bool check_circle(const node *head) //有环return true;无环 return false
{
if(NULL == head)
return false;
node *low=head, *fast=head->next;
while(NULL != fast && NULL != fast->next)
{
low = low->next;
fast = fast->next->next;
if(low == fast)
return true;
}
return false;
}node