判断链表中是否有环
方法:快慢指针
注意:链表功能需要自己添加,本题只做调用
bool hasCycle(struct ListNode* head ) {
// write code here
if(NULL == head) return false;
if(NULL == head->next) return false;
struct ListNode* slow = head->next;
struct ListNode* fast = head->next->next;
while(NULL != fast && NULL != fast->next)
{
if(slow == fast) return true;
slow = slow->next;
fast = fast->next->next;
}
return false;
}