题目:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
实现:
* 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* F = head;ListNode* S = head;
while(F != NULL)
{
if(F) F = F->next;
if(F) F = F->next;
if(S) S = S->next;
if(F != NULL && F == S)
{
return true;
}
}
return false;
}
};