Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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) {
vector<int> myVector;
int len_l = lists.size();
for(int i = 0; i < len_l; i++){
ListNode* temp = lists[i];
while(temp){
myVector.push_back(temp->val);
temp = temp->next;
}
}
sort(myVector.begin(), myVector.end());
ListNode* head = new ListNode(0);
ListNode* p_NewList = head;
len_l = myVector.size();
for(int i = 0; i < len_l; i++){
ListNode* temp = new ListNode(myVector[i]);
p_NewList->next = temp;
p_NewList = p_NewList->next;
}
return head->next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment