C++ code, no heap, no recursion, just sort....


  • -6
    L

    class Solution
    {

    public:
    
    ListNode* mergeKLists(vector<ListNode*>& list_vec)
    {   
        vector<int> tmp_vec;
    
        for (unsigned int i = 0; i < list_vec.size(); i++)
        {   
            while (list_vec[i])
            {   
                tmp_vec.push_back(list_vec[i]->val);
                list_vec[i] = list_vec[i]->next;
            }   
        }   
    
        sort(tmp_vec.begin(), tmp_vec.end());
    
        ListNode head(-1);
        ListNode* res_list = &head;
        ListNode* cur_node = res_list;
    
        for (unsigned int i = 0; i < tmp_vec.size(); i++)
        {   
            cur_node->next = new ListNode(tmp_vec[i]);
            cur_node = cur_node->next;
        }   
    
        return res_list->next;
    }   
    

    };


  • 0
    Y

    what can I say ....................................
    ............................................................


  • 0
    B

    Clever man as you are.
    I just want to know its running time.


Log in to reply
 

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