题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
#include <cstddef>
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
// 查找循环主要是注意返回值的内容是否相似
vector<ListNode*> ListA ;
//使用count判断是否在vector列表中,cout<<std::count(ListA.begin(), ListA.end(), pHead);
while(std::count(ListA.begin(), ListA.end(), pHead) == 0){
ListA.push_back(pHead);
pHead = pHead->next;
if(pHead == nullptr){
break;
}
}
return pHead;
}
};



查看3道真题和解析