Concise c++ solution use map


  • 3
    S
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            map<int,int> order;
            for(auto &list:lists){
                while(list!=NULL){
                    order[list->val]++;
                    list=list->next;
                }
            }
            ListNode head(0);
            ListNode* tmp=&head;
            for(auto &num:order){
                while(num.second--){
                    tmp->next=new ListNode(num.first);
                    tmp=tmp->next;
                }
            }
            return head.next;
        }
    };

Log in to reply
 

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