typedef struct LinkedList
{
int data;
LinkedList* pNext;
}LinkedList;
/**
* test 6, write a function to check whether there is loop in a linked list
* @param pList [input] linked list
* @return bool, if there is loop inside, return true, otherwise false
*/
bool CheckLoop(LinkedList* pList)
{
LinkedList* p1 = pList;
LinkedList* p2 = pList;
if (pList == NULL || pList->pNext == NULL)
return false;
while (p2 != NULL && p2->pNext != NULL)
{
p1 = p1->pNext;
p2 = p2->pNext->pNext;
if (p1==p2)
return true;
}
return false;
}
{
int data;
LinkedList* pNext;
}LinkedList;
/**
* test 6, write a function to check whether there is loop in a linked list
* @param pList [input] linked list
* @return bool, if there is loop inside, return true, otherwise false
*/
bool CheckLoop(LinkedList* pList)
{
LinkedList* p1 = pList;
LinkedList* p2 = pList;
if (pList == NULL || pList->pNext == NULL)
return false;
while (p2 != NULL && p2->pNext != NULL)
{
p1 = p1->pNext;
p2 = p2->pNext->pNext;
if (p1==p2)
return true;
}
return false;
}