题目
LeetCode - 142. Linked List Cycle II
题目链接
https://leetcode.com/problems/linked-list-cycle-ii/
参考博客
解题思路
解题源码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
unordered_set<ListNode*> s;
ListNode *detectCycle(ListNode *head) {
ListNode* p = head;
while(p){
if (s.count(p)) return p;
s.insert(p);
p = p->next;
}
return nullptr;
}
};