题解 | #链表中环的入口结点#
链表中环的入口结点
http://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) {
//遍历链表 map存储每个节点出现的次数 返回重复出现的第一个节点
unordered_map<ListNode*, int> m;
while(pHead != nullptr)
{
if(m[pHead] == 1)
return pHead;
m[pHead]++;
pHead = pHead->next;
}
return nullptr;
}
};
查看9道真题和解析
华为工作强度 1292人发布