Last active
August 29, 2015 14:06
-
-
Save clinyong/bd5a6d80679bd397253e to your computer and use it in GitHub Desktop.
快慢法判断链表有没有环
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 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