1:快慢链表(空间O1,时间On)
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while(fast!=NULL&&fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
if(slow==fast){
return true;
}
}
return false;
}
};
2:时间O1,空间O1
bool hasCycle(ListNode *head) {
int count = 10001;
while(count){
if(head==NULL)
return false;
head = head->next;
count--;
}
return true;
}



