Created
March 26, 2019 22:38
-
-
Save tuvo1106/9ae0c5cf80f2e90616f0c37022758fd5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
node *find_loop(node *head) | |
{ | |
node *p1; | |
node *p2; | |
if (head == NULL) | |
return (NULL); | |
p1 = head; | |
p2 = head; | |
while (p2->next != NULL && p2->next->next != NULL) | |
{ | |
p1 = p1->next; | |
p2 = p2->next->next; | |
if (p1 == p2) | |
{ | |
p1 = head; | |
while (p1 != p2) | |
{ | |
p1 = p1->next; | |
p2 = p2->next; | |
} | |
return (p2); | |
} | |
} | |
return (NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fundamental flaw of this implementation is if the first n items (n is Non zero) are equal to their index.
Can be fixed by skipping the items that equal their own index and then starting the rest of the progression.