O(1) space C++ solution. How do you solve it with extra space ? I couldnt think of anything.


  • 0
    S
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            ListNode* cur=head;
            while(cur&&cur->next!=head){
                cur=cur->next;
                if(cur)
                    cur=cur->next;
                else
                    break;
                head=head->next;
            }
            
            if(!cur)
                return 0;
            
            return 1;
        }
    };
    

Log in to reply
 

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