/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
void makell(ListNode* &head, int value)
{
ListNode* p=new ListNode();
p->val=value;
p->next=NULL;
ListNode* l=head;
while(l->next!=NULL)
{
l=l->next;
}
l->next=p;
}
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n1=lists.size();
multiset<int> s;
for(int i=0;i<n1;i++)
{
ListNode* a=lists[i];
while(a!=NULL)
{
s.insert(a->val);
a=a->next;
}
}
ListNode* head;
head=new ListNode();
head->val=0;
head->next=NULL;
for(auto it:s)
{
makell(head,it);
}
return head->next;
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter