/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL)return false;
if(head->next==NULL)return false;
ListNode *p=head;
ListNode *q=head->next;
while(p!=NULL&&q!=NULL)
{
if(p==q) return true;
p=p->next;
if(q->next==NULL)return false;
q=q->next->next;
}
return false;
}
};