Use vector to solve it


  • 0
    C

    i think its simple to understand,use space to exchange the time,
    here my codes:
    ListNode* mergeKLists(vector<ListNode*>& lists){
    vector<int> vals;
    for(int i=0;i<lists.size();i++){
    while(lists[i]){
    vals.push_back(lists[i]->val);
    lists[i]=lists[i]->next;
    }
    }
    sort(vals.begin(),vals.end());
    ListNode* start = new ListNode(0);
    ListNode* tail = start;
    for(int i=0;i<vals.size();i++){
    ListNode* p = new ListNode(vals[i]);
    tail->next = p;
    tail = p;
    }
    return start->next;
    }


Log in to reply
 

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