forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create detect_a_cycle_in_LinkedList.cpp
Detect a cycle in linked list using unordered set.
- Loading branch information
1 parent
acc38b8
commit 29a2b2a
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |