Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
经典的快慢指针
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * fast = head, *slow = head;
do{
if(fast==NULL|| fast->next==NULL) return false;
fast = fast->next->next;
slow = slow->next;
}while(fast != slow);
return true;
}
};