Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created November 10, 2018 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/e996a57a251dcbaa70b4a62590b5ba8e to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/e996a57a251dcbaa70b4a62590b5ba8e to your computer and use it in GitHub Desktop.
/**
* 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