Solve it without using extra spacing in C++


  • 2
    H
     class Solution {  
     public:  
          bool hasCycle(ListNode *head) {  
               if (!head)  
                   return false;  
               while (head->next && head->next->next) {
                   if (head == head->next->next)
                       return true;
                   else {
                       head = head->next;
                       head->next = head->next->next;
                   }
               }
               return false;
           } };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.