Little trick using c++


  • 0
    K

    Set a number of 10000, each traverse will increase the number by 1, by the time when the number is over 10000, and list still hasn't reached NULL point, then the list is considered cycled.

    bool hasCycle(ListNode *head) {
    if (head == NULL)
    {
    return false;
    }
    int n = 10000;
    int count = 0;
    while (head->next != NULL)
    {
    if (count >= n)
    {
    return true;
    }
    head = head->next;
    count++;
    }
    return false;

    }


Log in to reply
 

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