Skip to content

Commit

Permalink
Create detect_a_cycle_in_LinkedList.cpp
Browse files Browse the repository at this point in the history
Detect a cycle in linked list using unordered set.
  • Loading branch information
NirdeshGothania authored Oct 1, 2024
1 parent acc38b8 commit 29a2b2a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions detect_a_cycle_in_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool hasCycle(ListNode *head) {

ListNode* temp = head;
unordered_set<ListNode*> p;

while(temp != nullptr){

if(p.find(temp) != p.end()){
return true;
}

p.insert(temp);
temp = temp -> next;
}

return false;
}
};

0 comments on commit 29a2b2a

Please sign in to comment.