题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead) {
ListNode* res;
unordered_map<ListNode*, int> hash_table;
ListNode* pNode = pHead;
while (true) {
if (pNode == nullptr) {
return nullptr;
} else {
if (hash_table.find(pNode) == hash_table.end()) { //没找到
hash_table[pNode] = 1;
} else { //找到了
res = pNode;
break;
}
pNode = pNode->next;
}
}
return res;
}
};
联想公司福利 1500人发布
查看5道真题和解析