环形链表
代码目的:
给定一个链表,判断这个链表中是否有环。
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode*f=head;
ListNode*s=head;
if(f==NULL)
{return false;}
while(f!=NULL&&f->next!=NULL)
{
f=f->next->next;
s=s->next;
if(f==s)
{return true;}
}
return false;
}
};
代码的输出达到预期结果。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9545dc928240df3a2d044d4b5a432e1d.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0fb0198c3fd3594eaf2dad1789d722e1.png)