bool IsExitLoop(list *head)
{
list *slow = head, *fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
return !(slow == NULL || fast == NULL);
}
//找出环的入口
list *FindLoopPort(list *head)
{
list *slow = head, *fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (slow == NULL || fast == NULL)
return;
slow = head;
while (slow != fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
阅读(633) | 评论(0) | 转发(0) |