OJ-Problems-Source/LeetCode/141.cpp

15 lines
280 B
C++

class Solution {
public:
bool hasCycle(ListNode *head) {
if (head == nullptr || head->next == nullptr) return false;
ListNode* p = head;
ListNode* q = head->next->next;
while (p != q && q && q->next)
{
p = p->next;
q = q->next->next;
}
return (p == q);
}
};