//判断单链表中是否存在环
int IsExitCircle(Node* head)
{
Node *pslow=head,*pfast=head;
while(pfast && pfast->pnext)
{
pslow=pslow->pnext;
pfast=pfast->pnext->pnext;
if(pfast == pslow)
{
return true;
}
}
return false;
}
//环的长度,bomb为碰撞点
int CircleLen(Node* bomb)
{
int count=0;
Node *pslow=bomb,*pfast=bomb;
do
{
pslow=pslow->pnext;
pfast=pfast->pnext->pnext;
count++;
}while(pslow != pfast);
return count;
}
//求连接点
Node* ConnectPoint(Node *head,Node *bomb)
{
Node *p1=head,*p2=bomb;
while(p1 != p2)
{
p1=p1->pnext;
p2=p2->pnext;
}
return p1;
}
判断单链表中是否存在环
最新推荐文章于 2022-09-04 18:52:53 发布