一、哈希表(unordered_set)知识点
unordered_set是一种无序的数据集合容器,元素和键同时存在,元素没有按任何特定的顺序排序,而是根据它们的散列(hash)值组织成桶,以允许直接通过值快速访问各个元素(平均时间复杂度是O(1))。
方法说明和使用方法见:
unordered_set
二、题目
三、代码
/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}
};
*/
class Solution {
public:ListNode* EntryNodeOfLoop(ListNode* pHead) {unordered_set<ListNode*> s;while(pHead){if(s.count(pHead)>0) return pHead;s.insert(pHead);pHead=pHead->next;}return nullptr;}
};