I do not know why my answer got runtime error


  • 0
    L
    class mycomparison
    {
        bool reverse;
    public:
        mycomparison(const bool& revparam=false)
        {reverse=revparam;}
        bool operator() (const ListNode* lhs, const ListNode* rhs) const
        {
            if (reverse) return (lhs->val>rhs->val);
            else return (lhs->val<rhs->val);
        }
    };
    
    typedef std::priority_queue<int,std::vector<ListNode *>,mycomparison> mypq_type;
    
    class Solution {
    public:
        ListNode *mergeKLists(vector<ListNode *> &lists) {
            mypq_type myqueue(mycomparison(true));
            ListNode *head = new ListNode(0);
            ListNode *current = head;
            
            for(int i = 0;i < lists.size();i++)
                myqueue.push(lists[i]);
            
            while(!myqueue.empty()){
                ListNode* node = myqueue.top();
                myqueue.pop();
                current->next = new ListNode(node->val);
                current = current->next;
                if(node->next)
                    myqueue.push(node->next);
            }
            
            return head->next;
        }
        
    };

  • 0
    Y

    I got the same problem too.

    i runs well locally well get crashed when submit.


    ListNode* HelloWorld::mergeKLists(vector<ListNode *> &lists) {
    //  null
    for( int i=0; i<lists.size(); i++ ){
        if( lists[i] == NULL ){
            lists.erase(lists.begin()+i);
        }
    }
    
    if (lists.size()==0) {
        return NULL;
    }else if(lists.size()==1){
        return lists[0];
    }
    
    //  merge
    ListNode* outputHead = new ListNode(-1);
    ListNode* outputPoint = outputHead;
    ListNode* temp;
    
    vector<ListNode*> minv;
    
    for (int i=0; i<lists.size(); i++) {
        if( minv.size() == 0 ){
            minv.push_back(lists[i]);
            lists[i] = lists[i]->next;
            continue;
        }
        
        bool added = false;
        if(!(lists[i]->val > minv[0]->val)){
            minv.insert(minv.begin(), lists[i]);
            added = true;
        }
        if( !(lists[i]->val < minv[minv.size()-1]->val) && !added){
            minv.push_back(lists[i]);
            added = true;
        }
        
        if(!added){
            for(int j=0; j<minv.size(); j++){
                if( !(lists[i]->val > minv[j]->val) &&
                   (lists[i]->val < minv[j+1]->val)){
                    minv.insert(minv.begin()+j, lists[i]);
                    break;
                }
            }
        }
        lists[i] = lists[i]->next;
    }
    
    //  sort
    while (true) {
        outputPoint->next = minv[0];
        temp = minv[0];
        
        minv.erase(minv.begin());
        if(minv.size() == 0){
            outputPoint = outputPoint->next;
            break;
        }
        if( temp->next == NULL ){
            outputPoint = outputPoint->next;
            continue;
        }
    
        temp = temp->next;
        bool added = false;
        if(!(minv[0]->val < temp->val) ){
            minv.insert(minv.begin(), temp);
            added = true;
        }
        if( !(temp->val < minv[minv.size()-1]->val) && !added){
            minv.push_back(temp);
            added = true;
        }
        if ( !added ){
            for (int i=0; i<minv.size()-1; i++) {
                if( !(minv[i]->val>temp->val) && (minv[i+1]->val>temp->val)){
                    minv.insert(minv.begin()+i+1, temp);
                    break;
                }
            }
        }
        
        outputPoint = outputPoint->next;
    }
    
    return outputHead->next;
    

    }


Log in to reply
 

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