#include <stdbool.h>
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
bool hasCycle(struct ListNode* head ) {
// write code here
if((head->next==NULL) || (head == NULL))
{
return false;
}
struct ListNode *p1=head;
struct ListNode *p2=head;
while(1)
{
p1=p1->next;
p2=p2->next;
p2=p2->next;
if(p1 == p2)
{
return true;
}
if((p2 == NULL) || (p2->next==NULL))
{
return false;
}
}
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
bool hasCycle(struct ListNode* head ) {
// write code here
if((head->next==NULL) || (head == NULL))
{
return false;
}
struct ListNode *p1=head;
struct ListNode *p2=head;
while(1)
{
p1=p1->next;
p2=p2->next;
p2=p2->next;
if(p1 == p2)
{
return true;
}
if((p2 == NULL) || (p2->next==NULL))
{
return false;
}
}
}