Maybe the most straightforward c++ solution


  • 0
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            ListNode* head=new ListNode(0);
            ListNode* nxtptr=head;
            while(1){
                ListNode ** temp=nullptr;
                int smallest=INT_MAX;
                bool end=true;
                for(auto& x: lists){
                    if(x){
                        end=false;
                        if(x->val<smallest){
                            temp=&x;
                            smallest=x->val;
                        }
                    }
                }
                     if(end) break;
                    nxtptr->next=*temp;
                    nxtptr=nxtptr->next;
                    (*temp)=(*temp)->next;
            }
            return head->next;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.