Linked list cycle

here is the question of linked list for cycle detection

i am getting wrong answer in some of the test cases. can you tell my mistake
here is the code

ListNode *detectCycle(ListNode *head) {

    if(head==NULL)
    {
        return NULL;
    }
    ListNode* slow=head;
    ListNode* fast=head;
    while(fast!=NULL && fast->next!=NULL)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
           break;
        }
    }
    return slow->next;
    
}

};

Hey @aditikandhway, The starting point of the loop need not be slow->next, the intersection of fast and slow just represents that there’s a loop, however, to find the first node you’ll have to take one by one step along with head till slow and head meets.

ListNode detectCycle(ListNode head)
{
if(!head or !head->next) return NULL;
ListNode
slow=head;
ListNode
fast=head;
while(fast and fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast) break;
}
if(slow==fast)
{
while(slow!=head)
{
slow=slow->next;
head=head->next;
}
return head;
}
return NULL;
}
I’ve made some changes, hope it helps