给定个单链表,判断该链表是否存在环。
bool hasCycle(ListNode *head) {
if(head == NULL)
return false;
ListNode *fast = head;
ListNode *slow = head;
while(NULL != fast && NULL != fast->next)
{
fast = fast->next->next;
slow = slow->next;
if(slow == fast)
return true;
}
return false;
}【leetcode】Linked List Cycle,布布扣,bubuko.com
原文地址:http://blog.csdn.net/shiquxinkong/article/details/29689189