Skip to content

Instantly share code, notes, and snippets.

@clinyong
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clinyong/bd5a6d80679bd397253e to your computer and use it in GitHub Desktop.
Save clinyong/bd5a6d80679bd397253e to your computer and use it in GitHub Desktop.
快慢法判断链表有没有环
bool isLoop( link* head)
{
link* s = head; //移动缓慢的指针
link* f = head; //移动快速的指针
do
{
if( s == NULL ) return (false );
s = s->next; //一次向前移动一个
if( f== NULL ) return (false );
f = f->next;
if( f== NULL ) return (false );
f = f->next; //一次向前移动2个
}while( f!= s);
return (f!=NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment