Demo entry 6736457

233

   

Submitted by KQ on Apr 26, 2018 at 17:58
Language: C++. Code size: 857 Bytes.

/**
 * 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.empty())
            return NULL;
        vector<int> v;
        for(int i=0;i<lists.size();i++){
        	ListNode* temp=lists[i];
        	while(temp){
        		v.push_back(temp->val);
        		temp = temp->next;
        	}
        }
        if(v.empty())
        	return NULL;
        sort(v.begin(),v.end());
        ListNode* head;
        head=new ListNode(v[0]);
        ListNode* temp=head;
        for(int i=1;i<v.size();i++){
        	temp->next=new ListNode(v[i]);
        	temp=temp->next;
        }
        temp->next=NULL;
        return head;
    }
};

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).