mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
15 lines
280 B
C++
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);
|
|
}
|
|
};
|