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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode* pfast = head;
ListNode* pslow = head;
do{
if(pfast!=NULL)
pfast=pfast->next;
if(pfast!=NULL)
pfast=pfast->next;
if(pfast==NULL)
return false;
pslow = pslow->next;
}while(pfast != pslow);
return true;
}
};