TLE in C++ can anybody tell me why?

  • 0

    my code:

    class Solution {
            void reorderList(ListNode *head) {
                deque<ListNode*> temp;
                ListNode * cut = head;
                int n=temp.size();
                for(int i=0;i<n/2;++i){
                    temp[n-1-i]->next = temp[i]->next;
                    temp[i]->next = temp[n-1-i];

    please tell me why i got TLE!Thanks a lot!!

  • 0

    First of all, I see no point using a deque in your code as it stands now. All it does is random access and push_back(). vector might be a better choice in this scenario.

    I understand that your intention was to access elements from both ends of the container, but that thought was not reflected well in the implementation. It is better to use front(), back(), pop_front() and pop_back() to access only the head and tail.

  • 0

    Thanks a lot! However ,it still got tl.Maybe only the split way can meet the time limit

Log in to reply

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