```
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n = lists.size();
ListNode *ret = NULL, *p = NULL;
if(n)
{
vector<ListNode *> vec = lists;
int val = 0;
while(getSmallest(vec, val))
{
ListNode *tmp = new ListNode(val);
if(ret == NULL)
ret = p = tmp;
else
{
p->next = tmp;
p = tmp;
}
}
}
return ret;
}
bool getSmallest(vector<ListNode *> &vec, int &val)
{
int n = vec.size(), k = -1;
for(int i = 0; i < n; ++i)
{
if(vec[i] == NULL)
continue;
if(-1 == k || vec[i]->val > val)
{
val = vec[i]->val;
k = i;
}
}
if(k != -1)
vec[k] = vec[k]->next;
return -1 != k;
}
};
```