leetcode

23.合并K个升序链表

力扣链接

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
    	std::vector<int> vec;
    	for (auto it : lists){
    		ListNode* current = it;
    		while(current){
    			vec.push_back(current->val);
    			current =  current->next;
    		}
    	}
        if (vec.empty()) return nullptr;
    	std::sort(vec.begin(), vec.end());

    	ListNode* head = new ListNode(vec[0]);
    	ListNode* node = head;
    	for (int i = 1; i< vec.size(); i++){
    		node->next = new ListNode(vec[i]);
    		node = node->next;
    	}
    	return head;
    }
};