C++ O(0) Space Solution


  • 0

    When the list has a cycle, regardless of the step size, it always has a cycle
    I change the step size to 2, it will reduce the list, but will not damage the loop

    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            if (head == NULL) {
                return false;
            }
            while(head->next) {
                if(!head->next->next) {
                    break;
                }
                if (head->next == head->next->next) {
                    return true;
                }
                head->next = head->next->next;
                head = head->next;
            }
            return false;
        }
    };
    

Log in to reply
 

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