Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
bool hasCycle(ListNode *head) {//经典解法
ListNode* slow =head,*fast =head;
while(fast&&fast->next)
{
slow =slow->next;
fast =fast->next->next;
if(slow ==fast)
return true;
}
return false;
}
bool hasCycle(ListNode *head) {//经典解法
ListNode* slow =head,*fast =head;
while(fast&&fast->next)
{
slow =slow->next;
fast =fast->next->next;
if(slow ==fast)
return true;
}
return false;
}