Merging K Sort Lists

Keywords: less

Merge k sorting lists and return the merged sorting list. Please analyze and describe the complexity of the algorithm.

Example:

Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1 - > 1 - > 2 - > 3 - > 4 - > 4 - > 5 - > 6
Solution: Here is a violent solution, that is, all the sub-sequence nodes are put into a vector, using the custom sort function to arrange from small to large, and then output the sequence;
c++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
bool cmp(const ListNode*a,const ListNode*b){
    return a->val<b->val;
}
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        vector<ListNode*>temp;
        ListNode *head;
        for(int i=0;i<lists.size();i++){
            head = lists[i];
            while(head){
                temp.push_back(head);
                head = head->next;
            }
        }
        if(temp.empty()){
            return {};
        }
        //Use of sort function
        //sort algorithm with custom structure
        //At this time, we need to define a comparison function by ourselves.
        // Because the sort algorithm is based on the fact that elements in containers can be compared in pairs, and then sorted from small to large.
        sort(temp.begin(),temp.end(),cmp);//Attention must be paid here.
        
        for(int j=0;j<temp.size()-1;j++){
            temp[j]->next=temp[j+1];
        }
        temp[temp.size()-1]->next=NULL;
        return temp[0];
    }
};

Here's a complement to the C++ sort custom algorithm
In the second case, the sort algorithm is implemented with a custom structure.

At this time, we need to define a comparison function by ourselves, because the sort algorithm is based on the fact that the elements in the container can be compared in pairs, and then sorted from small to large, so we need to customize how to be less than ('<').

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
struct student{
    char name[10];
    int score;
};
//Customize "less than"
bool comp(const student &a, const student &b){
    return a.score < b.score;
}
int main(){
    vector<student> vectorStudents;
    int n = 5;
    while (n--){
        student oneStudent;
        string name;
        int score;
        cin >> name >> score;
        strcpy(oneStudent.name, name.c_str());
        oneStudent.score = score;
        vectorStudents.push_back(oneStudent);
    }
    cout << "===========Before sorting================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    sort(vectorStudents.begin(),vectorStudents.end(),comp);
    cout << "===========After sorting================" << endl;
    for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
        cout << "name: " << it->name << " score: " << it->score << endl;
    }
    return 0;
}

Posted by dkode on Thu, 03 Oct 2019 09:17:51 -0700