在这里插入代码片/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* faster{head};
ListNode* slower{head};
if(head==NULL)
return false;
while(faster!=NULL&&faster->next!=NULL)
{
faster=faster->next->next;
slower=slower->next;
if(faster==slower)
return true;
}
return false;
}
};
循环链表
最新推荐文章于 2023-12-27 14:53:05 发布
1770

被折叠的 条评论
为什么被折叠?



