# 单链表的环操做

2021年09月15日 阅读数：1

typedef int DataType;blog

typedef struct ListNodeget

{it

DataType data;io

struct ListNode *pNext;class

}SListNode, *PSListNode;List

{

PSListNode pNodeM = pMeetNode;

if (pHead == NULL || pMeetNode == NULL)

{

return NULL;

}

while (pNode1 != pNodeM)

{

pNode1 = pNode1->pNext;

pNodeM = pNodeM->pNext;

}

return pNode1;

}

int IsListCroseWithCycle(PSListNode pL1, PSListNode pL2)

{

PSListNode pL1MeetNode = NULL;

PSListNode pL2MeetNode = NULL;

PSListNode pL1Node = pL1;

PSListNode pL2Node = pL2;

PSListNode pNode = NULL;

if (pL1 == NULL || pL2 == NULL)

{

return 0;

}

pL1MeetNode = HasCycle(pL1Node);

pL2MeetNode = HasCycle(pL2Node);

//都不带环

if (pL1MeetNode == NULL && pL2MeetNode == NULL)

{

while (pL1Node->pNext != NULL)

{

pL1Node = pL1Node->pNext;

}

while (pL2Node->pNext != NULL)

{

pL2Node = pL2Node->pNext;

}

if (pL1Node == pL2Node)

{

return 1;

}

return 0;

}

//两个都带环

if (pL1MeetNode != NULL && pL2MeetNode != NULL)

pNode = pL1MeetNode;

{

while (pNode->pNext != pL1MeetNode)

{

if (pL2MeetNode == pNode)

{

return 1;

}

pNode = pNode->pNext;

}

return 0;

}

}