TheAlgorithms-C/leetcode/src/142.c

24 lines
548 B
C
Raw Normal View History

struct ListNode *detectCycle(struct ListNode *head)
{
2019-09-26 23:29:31 +08:00
if (head == NULL || head->next == NULL)
return NULL;
struct ListNode *slow, *fast;
slow = fast = head;
while (fast && fast->next)
{
2019-09-26 23:29:31 +08:00
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
2019-09-26 23:29:31 +08:00
struct ListNode *entry = head;
while (slow != entry)
{
slow = slow->next;
entry = entry->next;
2019-09-26 23:29:31 +08:00
}
return entry;
}
}
return NULL;
}