/**
* 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){
return false;
}
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && slow != NULL){
slow = slow->next;
fast = fast->next;
if(fast){
fast = fast->next;
}
else{
return false;
}
if(slow==fast){
return true;
}
}
return false;
}
}; 
京公网安备 11010502036488号