node *floyedDetectionLoop(node *head) {
if (head == NULL) {
return NULL;
}
node *fast = head;
node *slow = head;
while (fast != NULL && slow != NULL) {
fast = fast->next;
if (fast->next != NULL) {
fast = fast->next;
}
slow = slow->next;
if (fast == slow) {
return slow;
}
}
return NULL;
}
node *getStartingNode(node *head) {
if (head == NULL) {
return NULL;
}
node *intersection = floyedDetectionLoop(head);
node *slow = head;
while (slow != intersection) {
slow = slow->next;
intersection = intersection->next;
}
return slow;
}