/**
* 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)
{
ListNode*p=head;
ListNode*q=head;
if(p==NULL)
{
return 0;
}
while(p && q)
{
if(p->next && p->next->next)
{
p=p->next->next;
}
else
{
return 0;
}
if(q->next)
{
q=q->next;
}
else
{
return 0;
}
if(p==q)
{
return 1;
}
}
return 0;
}
};



京公网安备 11010502036488号