#include <stdbool.h>
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
typedef struct ListNode listnode;
bool hasCycle(struct ListNode* head ) {
// write code her
listnode*quick,*slow;
quick = slow = head;
while(quick && slow)
{
if(quick->next!=NULL)
{
quick = quick->next->next;
}
else
{
return false;
}
slow = slow->next;//快慢指针+碰撞指针
if(quick==slow)
{
return true;
}
}
return false;
}

京公网安备 11010502036488号