My O(1) space solution gets RunTime error - what am I missing?


  • 0
    S
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    bool hasCycle(struct ListNode *head) {
        
        if(head == NULL)
            return false;
            
        while(head->next)
        {
            if(head->next == head)
                return true;
            else
                head->next = head->next->next;
        }
        
        return false;
        
    }

  • 1
    B
    This post is deleted!

  • 0
    S

    Thank you - I did not think of it like that.


Log in to reply
 

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