【leetcode141】环形链表
快慢指针
/**
* 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;
ListNode *slow,*fast;
slow=head;
fast=head->next;
while(slow != NULL && fast != NULL){
if(fast==slow) return true;
slow=slow->next;
if(fast->next == NULL) break;
fast=fast->next->next;
}
return false;
}
};