knakul853

Untitled

Jul 17th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     ListNode *detectCycle(ListNode *head) {
  7.        
  8.        ListNode* slow = head;
  9.         ListNode* fast = head;
  10.         bool has = false;
  11.        
  12.         while(fast && fast->next )
  13.         {
  14.             fast = fast->next->next;
  15.              slow = slow->next;
  16.            
  17.             if(slow == fast){  has = true; break;}
  18.         }
  19.         if(!has) return NULL;
  20.         slow = head;
  21.         while(fast != slow )
  22.         {
  23.             fast = fast->next;
  24.             slow = slow->next;
  25.         }
  26.        
  27.        
  28.         return slow;
  29.        
  30.     }
  31. };
Add Comment
Please, Sign In to add comment