leetcode problem :23. Merge k Sorted Lists
Posted nosaferyao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode problem :23. Merge k Sorted Lists相关的知识,希望对你有一定的参考价值。
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; } };
以上是关于leetcode problem :23. Merge k Sorted Lists的主要内容,如果未能解决你的问题,请参考以下文章