Why does the solution cause Time Limit Exceeded?


  • 0
    J

    I do not know why the solution is TLE,because i think the time complexity is O(n).

     class Solution {
        public:
            ListNode *reverseBetween(ListNode *head, int m, int n) {
                if(n-m+1<=1) 
                    return head;
                ListNode *current=head;
                ListNode *prev=head;
                ListNode *temp=head;
                int i=1;
                while(i<m){
                    temp=prev=current;
                    current=current->next;
                    i++;
                }
                
                ListNode *next=NULL;
                while(i<=n){
                    next=current->next;
                    current->next=prev;
                    prev=current;
                    current=next;
                    i++;
                }
                temp->next->next=current;
                temp->next=prev;
                
                return head;
            }
        };

Log in to reply
 

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