Skip to content

Instantly share code, notes, and snippets.

@Krymancer
Created February 26, 2020 20:29
Show Gist options
  • Save Krymancer/5b0420aa6c111a4bb4308ad03b1ef010 to your computer and use it in GitHub Desktop.
Save Krymancer/5b0420aa6c111a4bb4308ad03b1ef010 to your computer and use it in GitHub Desktop.
Cycle Detection
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