Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active December 25, 2015 18:29
Show Gist options
  • Save MLLeKander/7021349 to your computer and use it in GitHub Desktop.
Save MLLeKander/7021349 to your computer and use it in GitHub Desktop.
findHead
Node findHead(Node start) {
int tortoiseSpeed = 0, hareSpeed = 0;
Node tortoise, hare;
if (start == null)
return start;
for (tortoiseSpeed = 0; true; tortoiseSpeed = hareSpeed + 1) {
if (start.jump(tortoiseSpeed+1) == null)
return null;
for (hareSpeed = 1; tortoiseSpeed >= 0; hareSpeed++, tortoiseSpeed--) {
tortoise = start.jump(tortiseSpeed);
hare = tortoise.jump(hareSpeed);
if (tortoise == hare)
return hare;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment