Created
June 20, 2014 01:11
-
-
Save gabhi/ff05fbc0a920eddc5a3b to your computer and use it in GitHub Desktop.
loop in the linked list
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
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