knakul853

Untitled

Jul 17th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     bool hasCycle(ListNode *head) { // for removing the node find starting node of the loop and set it's next as null.
  7.        
  8.         if( !head || !head->next ) return false;
  9.        
  10.         ListNode* slow = head;
  11.         ListNode* fast = head->next;
  12.        
  13.         while(slow != fast )
  14.         {
  15.             if( !fast || !fast->next ) return false;
  16.            
  17.             slow = slow->next;
  18.             fast = fast->next->next;
  19.         }
  20.        
  21.         return true;
  22.     }
  23. };
Add Comment
Please, Sign In to add comment