TLE - C++ code , ALGO - Using fast pointer, I'm checking will the head occur again


  • 0
    S

    I'm using a fast pointer and checking if it points head again, if linked list is a cycle it will otherwise it will not.

    enter code here

    bool hasCycle(ListNode *head) {
    
        ListNode *curr;
        
         if (head==NULL||head->next==NULL)
        {
           return false;
        }
        
        curr=head;
        
        while (curr!=NULL||curr->next!=NULL)
        {
            curr=curr->next->next;
            if (curr==NULL)
            {
                break;
            }
                    
            if (curr==head||curr->next==head)
            {
                return true;
            }
        }
        return false;
    }

  • 0

    Could you please format that properly instead of starting the code in your text paragraph?


  • 0
    S

    I have updated it


Log in to reply
 

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