/**
* 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) {
if(head==nullptr) return false;
unordered_map<ListNode *,bool> list;
ListNode *point=head;
while(list[point]!=true){
list[point]=true;//遍历过了就是true
point=point->next;
if(point==nullptr) return false;
}
return true;
}
};
leetcode 141. 环形链表 (判断链表是否有环)
最新推荐文章于 2021-09-12 15:40:08 发布