Why TLE? please help me.


  • 0
    S

    i got time limit exceeded:

    Submission Result: Time Limit Exceeded Last executed input:
    {3,2,0,-4}, tail connects to node index 1

    my code:

    class Solution {
    public:
        ListNode *detectCycle(ListNode *head) {
            ListNode *tmp;
            while(head!=NULL)
            {
                if(head==head->next)
                break;
                tmp=head->next;
                head->next=head;
                head=tmp;
            }
            return head;
        }
    };

  • 0
    F

    science the head will never be null,
    and head will never equal to head->next;

    when head point to -4 head->next will be the first element 3. you break will be never reached


  • 0
    S

    can you explain why heda will never be null and head will never equal to head->next?
    if there is no cycle head will be null and also i updated head->next = next.
    i got Ac Linked List Cycle I by using the same logic.


  • 0
    F

    Sorry, I was wrong before. After I tried your code on my computer, it's worked. However, I think that's maybe you have modified the input ListNodes , since LeetCode may need it.


Log in to reply
 

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