Advertisement
Guest User

Linked List Cycle

a guest
Jul 30th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1.     bool hasCycle(ListNode *head) {
  2.         if(!head){
  3.             return false;
  4.         }
  5.         ListNode* lento;
  6.         ListNode* rapido;
  7.         lento = rapido = head;
  8.         while(true){
  9.             lento = lento->next;
  10.             if(rapido->next){
  11.                 rapido = rapido->next->next;
  12.             }else{
  13.                 return false;
  14.             }
  15.             if(!lento || !rapido){
  16.                 return false;
  17.             }
  18.             if(lento == rapido){
  19.                 return true;
  20.             }
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement