vaibhav1906

linkedlist cycle 2

Nov 29th, 2021
893
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     ListNode *detectCycle(ListNode *head) {
  4.        unordered_set<ListNode*> s;
  5.        
  6.         while(head!=NULL){
  7.            
  8.             if(s.find(head)==s.end()){
  9.                 s.insert(head);
  10.             }
  11.             else{
  12.                 return head;
  13.             }
  14.             head = head->next;
  15.            
  16.            
  17.         }
  18.        
  19.         return NULL;
  20.     }
  21. };
RAW Paste Data