【LeetCode】HOT 100(4)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Topic: 23. Merge K ascending linked lists - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over, it's over! ! ! !

Write at the end:


Topic: 23. Merge K ascending linked lists - Leetcode

The interface of the topic:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {

    }
};

Problem-solving ideas:

It's great that this difficult problem isn't difficult,

It is the first difficult problem I have solved alone in my life.

I'm going to introduce two ideas,

One is the idea I used to do this question myself (he didn't ask for complexity, of course he can let himself go)

The other is the idea that I thought was wonderful when I read the solution.

Idea 1: (my idea)

1. Directly store the value of this linked list array into an array

2. Then sort the array

3. Build a linked list and return it

The idea is very simple, the code:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        vector<int> v; //将链表数组的值存进这个数组
        for(int i = 0; i < lists.size(); i++) {
            ListNode* cur = lists[i];
            while(cur) {
                v.push_back(cur->val);
                cur = cur->next;
            }
        }

        sort(v.begin(), v.end()); //排序

        ListNode* head = new ListNode; //构建链表
        ListNode* cur = head;
        head->next = nullptr;
        for(int i = 0; i < v.size(); i++) {
            ListNode* newnode = new ListNode;
            cur->next = newnode;
            cur = cur->next;
            cur->val = v[i];
        }
        cur->next = nullptr;
        return head->next;
    }
};

Then idea 2:

Using a priority queue, maintaining a small root heap,

However, the comparison of linked lists cannot be directly supported.

So we need to overload a comparison method ourselves.

1. Enqueue the linked list nodes

2. Take elements to build a new linked list

code show as below:

code:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    class Comparator { //重载的比较方法
    public:
        bool operator()(ListNode* a, ListNode* b) {
            return a->val > b->val;
        }
    };
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<ListNode*, vector<ListNode*>, Comparator> pq;
        for(int i = 0; i < lists.size(); i++) { //链表节点入队
            while(lists[i] != nullptr) {
                pq.push(lists[i]);
                lists[i] = lists[i]->next;
            }
        }
        ListNode* head = new ListNode; //构建链表
        ListNode* cur = head;
        while(!pq.empty()) {
            cur->next = pq.top();
            pq.pop();
            cur = cur->next;
        }
        cur->next = nullptr;
        return head->next;
    }
};

It's over, it's over! ! ! !

Write at the end: 

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/130861488