typedef struct Node
{
int data;
struct Node *next;
} STNode;
int IsCicleList(STNode *root)
{
STNode *p1 = root;
STNode *p2 = root;
while (p1 != NULL && p2->next != NULL)
{
p1 = p1->next;
p2 = p2->next->next;
if (p1 == p2)
{
return 1;
}
}
return 0;
}
int GetCicleLength(STNode *root)
{
STNode *p1 = root;
STNode *p2 = root;
int meetCount = 0;
int cicleLength = 0;
while (p1 != NULL && p2->next != NULL)
{
if (meetCount == 1)
{
cicleLength++;
}
p1 = p1->next;
p2 = p2->next->next;
if (p1 == p2)
{
meetCount++;
if (meetCount == 2)
{
break;
}
}
}
return cicleLength;
}
|