Linked List Cycle (Optimized)(Floyd Cycle)

PHOTO EMBED

Tue May 28 2024 22:21:18 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

bool hasCycle(ListNode *head) {
        ListNode *slow=head;
        ListNode *fast=head;
      while(fast!=NULL && fast->next!=NULL)
      {
        slow=slow->next;
        fast=fast->next->next;
        if(fast==slow)
        return true;
      }
      return false;
    }
content_copyCOPY

fast!=NULL && fast->next!=NULL Use this order only else it will give runtime error