class Solution { public: ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode* head = NULL; ListNode* current = NULL; for (int i = 0; i < lists.size(); ++i){ if (!lists[i]){ lists.erase(lists.begin()+i); --i; } } while (lists.size()){ int min_value=lists[0]->val; int min_index = 0; for (int i = 1; i < lists.size(); ++i){ if (lists[i]->val < min_value){ min_index = i; min_value = lists[i]->val; } } if (head == NULL){ head = lists[min_index]; current = head; } else { current->next = lists[min_index]; current = current->next; } lists[min_index] = lists[min_index]->next; if (!lists[min_index]){ lists.erase(lists.begin()+min_index); } } return head; } };
时间: 2024-11-05 22:48:26