问题描述如下:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
cpp代码如下:
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL)return false;
ListNode* f=head,*s=head;
do{
f=f->next;
if(!f)return false;
f=f->next;
if(!f)return false;
s=s->next;
}while(f!=s);
return true;
}
};