标签:splay gif close begin 反序 back lap init ==
------------恢复内容开始------------
1、暴力法
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeKLists(vector<ListNode*>& lists) { 12 //构建新的数组 13 vector<int> vec; 14 ListNode *head; 15 for(int i = 0; i < lists.size(); i++){ 16 head = lists[i]; 17 while ( head != nullptr){ 18 vec.push_back(head->val); 19 head=head->next; 20 } 21 22 } 23 //元素为空,后续就没有必要了 24 if (vec.size() == 0 ) return nullptr; 25 //排序,重构链表 26 sort(vec.begin(), vec.end()); 27 ListNode *curr,*temp; 28 ListNode *prev=new ListNode(0); 29 temp=prev; 30 // for (int i = vec.size()-1; i >= 0; i--){ //反序构造链表 31 // curr = new ListNode(vec[i]); 32 // curr->next = prev; 33 // prev = curr; 34 35 36 // } 37 for(int i=0;i<vec.size();i++) 38 { 39 curr=new ListNode(vec[i]); //正序构造链表 40 prev->next=curr; 41 prev=curr; 42 } 43 return temp->next; 44 45 } 46 };
2、分治法,归并排序
------------恢复内容结束------------
标签:splay gif close begin 反序 back lap init ==
原文地址:https://www.cnblogs.com/nxnslc-blog/p/12449601.html