答案:可设定两个指针,一个快指针pFast,每次走两步,一个慢指针pslow,每次走一步,然后让这两个指针从头开始遍历整个单向链表,如果链表中有环pFast与pSlow一定会相遇(因为链表没有结尾,没有NULL),否则不会相遇(都遍历到尾结点NULL)
int IsExistLoop(Linklist *head)
{
LinkList *pSlow = NULL;
LinkList *pFast = NULL;
while(pFast != NULL && pFast->next != NULL)
{
pSlow = pSlow->next;
pFast = pFast->next->next;
if (pSlow == pFast)
return 1;
}
return 0;
}