Advertisement
vaibhav1906

linkedlist cycle 2

Nov 29th, 2021
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement