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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter