CPP solution with no extra space


  • 1
    Q
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            if(head == NULL) return false;
            auto slow = head, fast = head;
            while(fast != NULL && fast -> next != NULL){
                slow = slow -> next;
                fast = fast -> next -> next;
                if(slow == fast) return true;
            }
            return false;
            
        }
    };

Log in to reply
 

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