Advertisement
vaibhav1906

LinkedList cycle 1

Nov 29th, 2021
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool hasCycle(ListNode *head) {
  4.        
  5.         unordered_set<ListNode*> s;
  6.        
  7.         while(head!=NULL){
  8.            
  9.             if(s.find(head)==s.end()){
  10.                 s.insert(head);
  11.             }
  12.             else{
  13.                 return true;
  14.             }
  15.             head = head->next;
  16.            
  17.            
  18.         }
  19.        
  20.         return false;
  21.        
  22.     }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement