Created
February 26, 2020 20:29
-
-
Save Krymancer/5b0420aa6c111a4bb4308ad03b1ef010 to your computer and use it in GitHub Desktop.
Cycle Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int detectLoop(Node * list) { | |
if (!list) | |
return 0; | |
Node * tortoise = list; | |
Node * hare = list; | |
do { | |
if (!(hare = hare->next) || !(hare = hare->next)) | |
return 0; | |
tortoise = tortoise->next; | |
} while (tortoise != hare); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment