Answers for "linked list cycle c++"

C++
0

linked list cycle c++

class Solution {
public:
    bool hasCycle(ListNode *head) {
        unordered_set<ListNode*> visited;
        if (head == NULL) {
            return false;
        }
        while (head) {
            if (visited.count(head)) {
                return true; // has appeared before
            }
            visited.insert(head);
            head = head->next;
        }
        return false;
    }
};
Posted by: Guest on April-06-2022
0

Linked List Cycle

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Posted by: Guest on February-03-2021

Browse Popular Code Answers by Language