标签:链表 lin 复杂 合并 leetcode nbsp title solution wap
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: static bool cmp(const ListNode *l1, const ListNode *l2) { return l1->val<l2->val; } ListNode *mergeKLists(vector<ListNode *> &lists) { int n = lists.size(); ListNode *res = new ListNode(-1); if (n<1) return res->next; if (n<2) return lists[0]; ListNode *p = res; int m = 0; for(int i=0;i<n;++i) { if(lists[i]==NULL) { swap(lists[m],lists[i]); m++; } } while(m < n-1) { sort(lists.begin()+m,lists.end(),cmp); p->next=lists[m]; p=p->next; lists[m]=lists[m]->next; if(lists[m]==NULL) { m++; } } if(lists[m]!=NULL) p->next=lists[m]; return res->next; } };
Leetcode merge-k-sorted-lists(合并k路有序链表 最小堆)
标签:链表 lin 复杂 合并 leetcode nbsp title solution wap
原文地址:https://www.cnblogs.com/zl1991/p/12797960.html