class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
};
class Solution {
public:
bool hasCycle(ListNode *head) {
std::unordered_set<ListNode*> uset;
ListNode* current = head;
while(current){
if (uset.find(current)!=uset.end()) return true;
uset.insert(current);
current = current->next;
}
return false;
}
};