Another solution.
Traverse the list , and if I find a node point to itself, it means the list has a cycle.
Otherwise, I will make the checked node point to itself.
But I break the list,it is just an idea.
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 *tmp;
while(head!=NULL){
if(head->next == head) return true;
tmp=head->next;
head->next=head;
head=tmp;
}
return false;
}
};