LCR 078. 合并 K 个升序链表
题目链接:LCR 078. 合并 K 个升序链表
代码如下:
class Solution {
public:ListNode* mergeKLists(vector<ListNode*>& lists) {ListNode *ls=nullptr;for(int i=0;i<lists.size();i++){ls=mergeList(ls,lists[i]);}return ls;}ListNode* mergeList(ListNode* a,ListNode* b){if(a&&!b)return a;if(!a&&b)return b;ListNode* head=new ListNode;head->next=nullptr;ListNode* r=head;while(a&&b){if(a->val<b->val){r->next=a;a=a->next;}else{r->next=b;b=b->next;}r=r->next;}if(a)r->next=a;if(b)r->next=b;return head->next;}
};