环形链表

 

/**
 * 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*> c;
        while(head != NULL){
            if(c.count(head)){
                return true;
            }
            c.insert(head);
            head = head -> next;
        }
        return false;
    }
};
posted @ 2023-05-09 19:40  Hbro  阅读(13)  评论(0)    收藏  举报