十二:判断单链表是否有环

快慢指针
// 判断链表中是否有环
bool IsExitLoop(LinkList *head)
{
    LinkList *pSlow = head;
    LinkList *pFast = head;
    while (pFast != NULL && pFast->next != NULL)
    {
        pSlow = pSlow->next;
        pFast = pFast->next->next;
        if (pSlow == pFast)
        {
            return true;
        }
    }
    return false;
}

猜你喜欢

转载自blog.csdn.net/ndzjx/article/details/84839565