Need clarification on the below code. why we are using fast_p->next in while loop.. thanks in advance


  • -1
    D

    public:
    bool hasCycle(ListNode *head) {
    if(!head)
    return false;
    ListNode * slow_p,*fast_p;
    slow_p=head;
    fast_p=head->next;
    while(slow_p&&fast_p&&fast_p->next)
    {
    if(slow_p==fast_p)
    return true;
    slow_p=slow_p->next;
    fast_p=fast_p->next->next;
    }
    return false;
    }


Log in to reply
 

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