LeetCode 141

/**
 * 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面试经典,leetcode,算法,职场和发展)