```
class Solution {
public:
bool hasCycle(ListNode *head) {
if (!head) return false;
if (!head->next) return false;
ListNode* fast = head->next;
ListNode* slow = head;
while (fast->next) {
if (fast==slow || fast->next==slow)
return true;
fast=fast->next->next;
slow=slow->next;
}
return false;
}
};
```

the idea is to use two pointers: slow and fast. The slow moves in the list one by one while the fast moves by skipping a node each time. If there is a cycle, eventually the fast and slow will meet each other.

But this code fails. Can anyone help?