/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return bool布尔型
*/
bool hasCycle(ListNode* head) {
// write code here
// 每只牛编号唯一
set<int> s;
while(head)
{
// 有环
if(s.count(head->val)>0)
return true;
s.emplace(head->val);
head = head->next;
}
return false;
}
};

京公网安备 11010502036488号