mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
24 lines
548 B
C
24 lines
548 B
C
struct ListNode *detectCycle(struct ListNode *head)
|
|
{
|
|
if (head == NULL || head->next == NULL)
|
|
return NULL;
|
|
struct ListNode *slow, *fast;
|
|
slow = fast = head;
|
|
while (fast && fast->next)
|
|
{
|
|
slow = slow->next;
|
|
fast = fast->next->next;
|
|
if (slow == fast)
|
|
{
|
|
struct ListNode *entry = head;
|
|
while (slow != entry)
|
|
{
|
|
slow = slow->next;
|
|
entry = entry->next;
|
|
}
|
|
return entry;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|