Skip to content

Instantly share code, notes, and snippets.

@BlackHC
Last active December 15, 2015 16:18
Show Gist options
  • Select an option

  • Save BlackHC/5287680 to your computer and use it in GitHub Desktop.

Select an option

Save BlackHC/5287680 to your computer and use it in GitHub Desktop.
bool hasCycle( Element *head ) {
Element *guard = head;
Element *current = head;
bool updateGuard = false;
while( current ) {
current = current->next;
if( current == guard ) {
return true;
}
if( updateGuard ) {
guard = guard->next;
}
updateGuard = !updateGuard;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment