23. Merge k Sorted Lists

PHOTO EMBED

Sat Feb 11 2023 11:58:50 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

/**
 * 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;
    }
};
content_copyCOPY

https://leetcode.com/problems/merge-k-sorted-lists/