Runtime Error on [{1}]


  • 0
    G

    I copied the code into another online coding editor it runs fine but I kept receive
    this on Online Judge.
    Runtime Error
    Last executed input: [{1}]

    here is my code, any idea how to debug on OJ? thanks!

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *mergeKLists(vector<ListNode *> &lists) {
            if(lists.size()==0) return NULL;
            vector<int> sortval;
            ListNode* cur;
            for(int i = 0;i<(int)lists.size();i++){
                cur = lists[i];
                while(cur!=NULL){
                    sortval.push_back(cur->val);
                    cur = cur->next;
                }
            }
            cout << "here";
            ListNode* root;
            std::sort(sortval.begin(),sortval.end());
            
            for(int i = 0;i<(int)sortval.size();i++){
                for(int j = 0;j<(int)lists.size();j++){
                    if(lists[j]!=NULL&&lists[j]->val==sortval[i]){
                        if(i==0){
                            root = lists[j];
                            cur = root;
                            lists[j] = lists[j]->next;
                        }
                        else{
                            cur->next = lists[j];
                            lists[j] = lists[j]->next;
                            cur = cur->next;
                        }
                        break;
                    }
                }
            }
            return root;
        }
    };

  • 2
    S

    Probably this code fails on test case [] instead of [{1}].

    If the code initializes the root by ListNode* root = NULL;, it will time limit exceeded.


  • 0
    G

    thanks a lot!


Log in to reply
 

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