C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
unordered_map<ListNode*,int> pos;
ListNode *entryNodeOfLoop(ListNode *head) {
for(ListNode*p=head;p;p=p->next){
if(pos[p]==1){
return p;
}else{
pos[p]++;
}
}
return NULL;
}
};