Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
    }
};
Comment

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).
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ forbids comparison between pointer and integer 
Cpp :: il2cpp stuck unity 
Cpp :: c++ find index of element in array 
Cpp :: qt make widget ignore mouse events 
Cpp :: vector from angle 
Cpp :: rock paper scissor c++ 
Cpp :: one away coding question 
Cpp :: friend function in c++ 
Cpp :: oncomponentendoverlap ue4 c++ 
Cpp :: C++ linked list iterator 
Cpp :: Initialize Vector Iterator 
Cpp :: What is a ~ in c++ 
Cpp :: set size of a vector c++ 
Cpp :: C++ cout iostream 
Cpp :: c++ pass ofstream as argument 
Cpp :: how to make randomizer c++ 
Cpp :: c++ split string 
Cpp :: int cpp 
Cpp :: Start mongodb community server 
Cpp :: C++ vector at() method 
Cpp :: concatenate string in cpp 
Cpp :: c++ switch case 
Cpp :: assign value to a pointer 
Cpp :: use declaration to define a variable 
Cpp :: c++ visual studio 
Cpp :: bool nullable to bool c# 
Cpp :: pointer to value of others file cpp 
Cpp :: even number program in c++ using for loop stack overflow 
Cpp :: 3. The method indexOf, part of the List interface, returns the index of the first occurrence of an object in a List. What does the following code fragment do? 
Cpp :: triangle angle sum 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =