您的位置:首页 > 其它

如何判断 单链表中是否存在环 ( How judges in Singly Linked List whether has a circle )

2007-05-21 14:59 399 查看
class Node

{

 int data;

Node * link;

 }

思想:用2个指针 1个步进一 1个步进2  如果存在环 慢指针 必定能追上快指针

bool check(const Node * head)

{

Node * low=head; Node * fast=head->link->link;

while(low!=NULL&&fast!=NULL){

 if(low==fast) return true;

}

return false;

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  list class null