mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-23 05:52:21 +03:00
20 lines
525 B
C
20 lines
525 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;
|
|
}
|