哎,这两天都是在做水题,做水题都还一点状态都木有,
经常一道题调好久~~~
哎,好拙计!
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 // Start typing your C/C++ solution below 13 // DO NOT write int main() function 14 if (lists.empty()) { 15 return NULL; 16 } 17 ListNode *res = NULL; 18 ListNode *pre; 19 ListNode *cur; 20 vector<ListNode *> prelist = lists; 21 bool first = false; 22 while (!prelist.empty()) { 23 bool val = false; 24 vector<ListNode *> curlist; 25 for (size_t i = 0; i < prelist.size(); ++i) { 26 if (prelist[i] == NULL) { 27 continue; 28 } 29 if (!val) { 30 val = true; 31 cur = prelist[i]; 32 continue; 33 } 34 if (prelist[i]->val < cur->val) { 35 curlist.push_back(cur); 36 cur = prelist[i]; 37 } 38 else { 39 curlist.push_back(prelist[i]); 40 } 41 } 42 if(!val) { 43 if (res == NULL) { 44 return NULL; 45 } 46 break; 47 } 48 curlist.push_back(cur->next); 49 prelist = curlist; 50 if (!first) { 51 first = true; 52 res = cur; 53 pre = cur; 54 } 55 else { 56 pre->next = cur; 57 pre = cur; 58 } 59 } 60 if (res == NULL) { 61 return res; 62 } 63 cur->next = NULL; 64 return res; 65 } 66 };