My 26ms cpp codes, use vector and sort function.


  • 0
    1

    class Solution {
    public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
    vector<int> values;
    for (int i = 0; i < lists.size(); ++i)
    {
    ListNode * p = lists[i];
    while(p != NULL){
    values.push_back(p->val);
    p = p->next;
    }
    }
    sort(values.begin(), values.end());
    ListNode * head = new ListNode(0);
    ListNode * p = head;
    for (int i = 0; i < values.size(); ++i)
    {
    ListNode * temp = new ListNode(values[i]);
    p->next = temp;
    p = p->next;
    }
    if(head == NULL)cout << 1;
    head = head->next;

    	return head;
    }
    

    };


Log in to reply
 

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