/**
- struct ListNode {
- int val;
- struct ListNode *next;
- };
- C语言声明定义全局变量请加上static,防止重复定义 / /*
- 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
- @param pHead ListNode类
- @return ListNode类
/
#include <stdbool.h>
bool hasCycle(struct ListNode head ) {
// write code here
if(head == NULL) return false;
struct ListNode p = head;
for(int i = 1;i<10000;i++)
{
p = p->next;
if(p == NULL) return false;
}
return true;
}
struct ListNode EntryNodeOfLoop(struct ListNode* pHead ) {
// write code here
if(hasCycle(pHead) == false) return NULL;
struct ListNode *p1,*p2;
p1 = pHead;
int count = 0;
for(int i = 1;i<= 10000;i++)
p1 = p1->next;
p2 = p1;
do
{
count++;
p1 = p1->next;
}while(p1 != p2);
p2 = p1 = pHead;
do
{
p2 = p1;
for( int i = 1;i<=count;i++)
{
p2 = p2->next;
} if(p2 == p1) return p2; else p1 = p1->next; }while(1);
}