在这里插入代码片/**
* 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;
}
};
循环链表
最新推荐文章于 2024-09-29 14:28:46 发布