```
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *p,*q;
p=head;
if(p==NULL) return false;
if(p->next==NULL) return false;
while(p->next!=NULL){
q=p->next;
p->next=p;
if(p==q) return true;
else p=q;
}
return false;
}
};
```