Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created March 26, 2019 22:38
Show Gist options
  • Save tuvo1106/9ae0c5cf80f2e90616f0c37022758fd5 to your computer and use it in GitHub Desktop.
Save tuvo1106/9ae0c5cf80f2e90616f0c37022758fd5 to your computer and use it in GitHub Desktop.
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);
}
@fcarelse
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment