class Solution {
public:
bool hasCycle(ListNode *head) {
if (head == nullptr || head->next == nullptr)
return false;
ListNode * slow = head, * fast = head->next;
while (fast != nullptr && fast->next != nullptr)
{
if (fast == slow)
return true;
slow = slow->next;
fast = fast->next->next;
}
return false;
}
};
京公网安备 11010502036488号