题目描述:
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 * fast = head;
ListNode * slow = head;
while(fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
if(fast == slow)
break;
}
if(fast == NULL || fast->next == NULL)
return false;
else
return true;
}
};原文地址:http://blog.csdn.net/yao_wust/article/details/41011719