Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created June 20, 2014 01:11
Show Gist options
  • Save gabhi/ff05fbc0a920eddc5a3b to your computer and use it in GitHub Desktop.
Save gabhi/ff05fbc0a920eddc5a3b to your computer and use it in GitHub Desktop.
loop in the linked list
bool hasLoop(list *head)
{
list *slow, *fast;
slow = fast = head;
while (slow && fast )
{
slow = slow->next;
fast = fast->next;
if (fast)
fast = fast->next;
else
return false; //dead end
if (slow == fast) //loop detected
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment