//带环返回1
//不带环返回0
int IsCycle(PLinkList pList, PLinkList*ppMeetNode)
{
Node*fast = pList;
Node*slow = pList;
while (fast&&fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
*ppMeetNode = slow;
return 1;
}
}
*ppMeetNode = NULL;
return 0;
}
int GetCycleLength(PLinkList pMeetNode)
{
assert(pMeetNode);
Node*begin = pMeetNode;
int count = 1;
assert(pMeetNode);
while (begin)
{
if (begin->next == pMeetNode)
{
return count;
}
begin = begin->next;
++count;
}
return 0;
}
原文地址:http://10622551.blog.51cto.com/10612551/1689605