#include <stdbool.h>
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
bool hasCycle(struct ListNode* head ) {
// write code here
struct ListNode *fastpointer=head,*slowpointer =head;
while(fastpointer!=NULL&&fastpointer->next!=NULL)
{
fastpointer=fastpointer->next->next;
slowpointer=slowpointer->next;
if(slowpointer==fastpointer)
{
return true;
}
}
return false;
}

京公网安备 11010502036488号