LeetCode 141.环形链表
给定一个链表,判断链表中是否有环。
思路:快慢指针
bool hasCycle(ListNode *head) {
ListNode *fast = head, *slow=head;
while(fast!=NULL && slow!=NULL && fast->next !=NULL){
fast = fast->next->next; //快指针
slow = slow->next; //慢指针
if(fast == slow)
return true;
}
return false;
}