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 isExistLoop(LinkedList* pList)
{
if (pList == NULL || pList->pNext == NULL)
return false;
LinkedList* p1 = pList;
LinkedList* p2 = pList;
while (p2 != NULL && p2->pNext != NULL)
{
p1 = p1->pNext;
p2 = p2->pNext->pNext;
if (p1==p2)
return true;
}
return false;
}
判断单链表是否有环?
最新推荐文章于 2024-08-20 12:43:06 发布