Leetcode 23. Merge k Sorted Lists (Heap | | Divide and conquer law)

Source: Internet
Author: User

Merge k sorted linked lists and return it as one sorted list.

Test Instructions : The K has been ordered into a list of linked lists, and the list is ordered.

problem: This is a classic good question, it is worth to say carefully.

There are two methods, assuming that the average length of each list is n, the time complexity of both methods is O (NKLOGK).

Method One :

The basic idea is that the value of the beginning of the K list is ordered, each time the smallest value is placed in the answer list, this time after the completion of the update value is a value after it. Then take it all the way to the end. So how do you reorder the current K values after each update so that you know who is the smallest one now? The priority queue (or heap) to maintain the K value is good!

Since each value is to be taken once, take NK times altogether. The complexity to logk each time the priority queue is updated. So the total time complexity is O (NKLOGK), space complexity is the priority queue occupied space, is O (k).

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classsolution{ Public:    structCMP {BOOL operator() (ConstListnode* A,Constlistnode*b) {returnA->val > B->Val;    }    }; ListNode* Mergeklists (vector<listnode*>&lists) {Priority_queue<listnode*, Vector<listnode*>, cmp>PQ;  for(Auto i:lists) {if(i)//This judgment is necessary to join the queue without empty. such as this set of data: [[[],[]]{Pq.push (i); }        }        if(Pq.empty ()) {returnnullptr; } ListNode* ans =Pq.top ();        Pq.pop (); ListNode* Tail =ans; if(tail->next) {Pq.push (tail-next); }         while(!Pq.empty ()) {Tail->next =Pq.top (); Tail= tail->Next;            Pq.pop (); if(tail->next) {Pq.push (tail-next); }        }        returnans; }};

Method Two:

And method One of the different ideas: Consider the idea of divided treatment to solve the problem (similar to the idea of merging sort). Divide the list into two halves, and if each half is merged, then I will finally merge the two. This is the core idea of the division of Law.

However, because the problem is the pointer, it has greater operational flexibility, can not be handed back to achieve division. is the first 22 merge after merging in 22 ... All the way down until the end. (The binary tree that is equivalent to the divide-and-conquer algorithm goes up from the bottom).

The first 22 merges were performed K/2 times, each processing 2n values.

The second 22 merge is performed K/4 times, each processing 4n values.

。。。

The last 22 merges were performed k/(2^LOGK) times, each processing 2^logk*n values.

So the complexity of time:

O ((2N) * (K/2) + (4N) * (K/4) + (8N) * (K/8) + ..... + () + (2^logk*n) * (K/(< Span class= "number Cye-lm-tag" >2 ^logk)) =o (logk *kn)

Space complexity is O (1).

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*mergeklists (Vector<listnode *> &lists) {    if(Lists.empty ()) {returnnullptr; }     while(Lists.size () >1) {lists.push_back (mergetwolists (lists[0], lists[1]));        Lists.erase (Lists.begin ());    Lists.erase (Lists.begin ()); }    returnLists.front (); } ListNode*mergetwolists (ListNode *l1, ListNode *L2) {        if(L1 = =nullptr) {            returnL2; }        if(L2 = =nullptr) {            returnL1; }        if(L1->val <= l2->val) {L1->next = Mergetwolists (l1->Next, L2); returnL1; }        Else{L2->next = mergetwolists (L1, l2->next); returnL2; }    }};

Leetcode 23. Merge k Sorted Lists (Heap | | Divide and conquer law)

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.