Posted on

Description

Submission

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode*> Set;

        for(ListNode* ptr = head; ptr != NULL; ptr = ptr->next) {
            if(Set.find(ptr) != Set.end()) {
                return ptr;
            }
            Set.insert(ptr);
        }
        return NULL;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *