Leetcode merge K sorted lists

Source: Internet
Author: User

MergeKSorted linked lists and return it as one sorted list. analyze and describe its complexity.

Merge K sort lists

The solution is as follows:

Fetch k elements for heap sorting, fetch the smallest elements each time, and insert them into the linked list.

Note that this question uses the priority queue of C ++. The underlying priority queue is implemented by heap.

Note that a special test case in this question is [{}]

Note that when the vector is null, it is [] instead of [{}], while the latter is a vector with a size of 1, where the element is a null pointer.

When put into the priority queue, determine whether it is empty

The element in the priority queue is listnode * instead of Int. It mainly maintains the address of the original linked list to avoid address failure. In addition, if it is put into int, the new listnode (INT) is added) the overhead is large.

Define a comparison function object for listnode * in the priority queue. If not, compare the address size.

class Solution {public:    struct cmp{        bool operator()(const ListNode* a, const ListNode* b) const{            return a->val > b->val;        }    };        ListNode *mergeKLists(vector<ListNode *> &lists) {        if(lists.empty()) return NULL;        priority_queue<ListNode*,vector<ListNode*>,cmp> pq;        ListNode *head = new ListNode(0), *pre = head;        for(int i = 0 ; i < lists.size(); ++ i){            if(lists[i]) pq.push(lists[i]);        }        while(!pq.empty()){            ListNode* node = pq.top(); pq.pop();            if(node->next) pq.push(node->next);            pre->next = node;            pre = node;        }        pre->next = NULL;        return head->next;    }};

The second method is the binary method. Each time two listnode * is obtained for merging.

The program uses a queue. Each time two elements are extracted from the queue and then merged, the merged elements are put into the queue until there is only one small element left in the queue.

class Solution {public:    ListNode *mergeLists(ListNode* p,ListNode* q){        ListNode* head = new ListNode(0),*pre = head;        while(p && q){            if(p->val < q ->val){                ListNode *tmp = p->next;                p->next = pre->next;                pre->next = p;                pre = p;                p = tmp;            }else{                ListNode *tmp = q->next;                q->next = pre->next;                pre->next = q;                pre = q;                q = tmp;            }        }        pre->next = NULL;        if(p) pre->next = p;        if(q) pre->next = q;         return head->next;    }    ListNode *mergeKLists(vector<ListNode *> &lists) {        if(lists.empty()) return NULL;        queue<ListNode *> que;        for(int i = 0 ; i < lists.size(); ++ i){            if(lists[i]) que.push(lists[i]);        }        while(que.size()>1){            ListNode* p = que.front();que.pop();            ListNode* q = que.front();que.pop();            que.push(mergeLists(p,q));        }        return que.front();    }};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.