Time Limit exceeded


  • 0
    V

    I got a time limit exceed, and I don't why. May somebody help me?
    The following is my code (C++). Really appreciate it!
    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {
    if(head==NULL||head->next==NULL)
    return head;
    ListNode* tmp1=head;
    ListNode* tmp2=head->next;
    ListNode* tmp3=NULL;
    while(tmp2){
    tmp2->next=tmp1;
    tmp1=tmp2;
    tmp2=tmp3;
    }
    return tmp1;
    }
    };


  • 0
    L

    the codes below is written based on your codes and they are accepted by the judge, maybe you can find the mistakes of your codes

    ListNode* reverseList(ListNode* head) {
    if(head==NULL||head->next==NULL)
        return head;
    
    ListNode* tmp1=head;
    ListNode* tmp2=head->next;
    ListNode* tmp3=NULL;
    while(tmp2){
        tmp1->next = tmp3;
        tmp3 = tmp1;
        tmp1 = tmp2;
        tmp2 = tmp1->next;
    }
    tmp1->next = tmp3;
    return tmp1;}

Log in to reply
 

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