OJ-Problems-Source/LeetCode/141.cpp

15 lines
280 B
C++
Raw Normal View History

2018-08-13 22:33:47 +08:00
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);
}
};