mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-21 12:53:13 +03: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;
|
|
}
|