/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_set<ListNode *>store;
ListNode *t=head;
while(t)
{
if(store.count(t))
{
return true;
}
store.insert(t);
t=t->next;
}
return false;
}
};
LeetCode 141. 环形链表
2024-01-06 05:42:03 39 阅读