环形链表
分析
快慢指针。
class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head||head->next==NULL)return false;
ListNode * fast=head->next;
ListNode * slow=head;
while(fast!=slow){
if(fast==NULL||fast->next==NULL)return false;
slow=slow->next;
fast=fast->next->next;
}
return true;
}
};