Untitled

 avatar
unknown
plain_text
10 months ago
430 B
4
Indexable
bool hasCycle(struct ListNode *head) {
    if (!head)
    {
        return false;
    }
    struct ListNode *fastTravel = head;
    struct ListNode *slowTravel = head;

    while(fastTravel && fastTravel->next)
    {
        fastTravel = fastTravel->next->next;
        slowTravel = slowTravel->next;

        if(slowTravel == fastTravel)
        {
            return true;
        }
    }

    return false;
}
Editor is loading...
Leave a Comment