I dont know what this message means...who can tell me ?


  • 0
    Z

    (0_1467791217724_QQ截图20160706153914.jpg
    this is my code:

    • bool hasCycle(ListNode *head) {
        if(head == NULL) return false;
        if(head->next == NULL) return false;
        ListNode *back = head;
        ListNode *front = head;
        while(front)
        {
            front = front->next;
            back = head;
            while(back != front)
            {
                if(back == front->next)
                    return true;
                back = back->next;
            }
        }
        return false;
      
      }

    WHY?


  • 0

    Consider the test case where the list has only one element, the head, and the head's next pointer points to itself, i.e, head->next = head;
    Your inner while loop will never be entered, because front always equals back.
    Your outer while loop will be endless because front will always be the head, which is not null.
    So, you have TLE.


  • 0
    Z

    @douglasleer i got it .thank you so much.


Log in to reply
 

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