23. 合并K个排序链表

 1 class Solution 
 2 {
 3 public:
 4     ListNode* mergeKLists(vector<ListNode*>& lists) 
 5     {
 6         vector<int> nums;
 7         for(auto a : lists)
 8         {
 9             ListNode* head = a;
10             while(head)
11             {
12                 nums.push_back(head->val);
13                 head = head->next;
14             }
15         }
16         sort(nums.begin(),nums.end());
17         ListNode* dummy = new ListNode(-1);
18         ListNode* pre = dummy;
19         for(auto a : nums)
20         {
21             ListNode* temp = new ListNode(a);
22             pre->next = temp;
23             pre = pre->next;
24         }
25         return dummy->next;
26     }
27 };

 

上一篇:6. 从尾到头打印链表[java]


下一篇:24. Swap Nodes in Pairs