Leetcode oj:merge k Sorted Lists (merge K list)

Source: Internet
Author: User

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Similar to merging 2 lists, the brute force approach is to return the merge list to the list after each list is removed, and to know that all the list merges are complete.

But unfortunately, this will be tle. Paste the code First:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: Onelistnode* mergeklists (vector<listnode*>&lists) { AListNode * ret =NULL; -          for(Auto *it:lists) { -RET =Mergelist (ret, it); the         } -         returnret; -     } -  +ListNode * Mergelist (ListNode * head1, ListNode *head2) -     { +         if(!HEAD1)returnhead2; A         if(!HEAD2)returnHead1; atListNode * head =NewListNode (-1); -Head->next =Head1; -ListNode * prev =head; -ListNode * Tmpnode =NULL; -          while(Head1 &&head2) { -             if(Head1->val < head2->val) { inPrev = prev->Next; -Head1 = head1->Next; to}Else{ +Tmpnode = head2->Next; -Prev->next =head2; theHead2->next =Head1; *Prev =head2; $Head2 =Tmpnode;Panax Notoginseng             } -         } the         if(head2) { +Prev->next =head2; A         } the         returnHead->Next; +     } -};

The following is done with a heap, first build a small heap, find little elements. Merge it into a linked list. If there are elements behind it, then put them in the heap. The code is as follows:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: One     Static BOOLComp (ListNode * L1, ListNode *L2) A     { -         if(L1->val < l2->val) -             return false; the         return true; -     } -  -listnode* mergeklists (vector<listnode*>&lists) { +Vector<listnode *>removenulllist; - Removenulllist.reserve (Lists.size ()); +          for(Auto it = Lists.begin (); It! = Lists.end (); + +it) { A             if(*it! =NULL) atRemovenulllist.push_back (*it); -         } -         if(removenulllist.size () = =0)returnNULL;//first remove the null node - make_heap (Removenulllist.begin (), Removenulllist.end (), Comp); -ListNode * Helper =NewListNode (-1); -ListNode * Tail =Helper; inListNode * Minnode =NULL; -          while(!Removenulllist.empty ()) { to pop_heap (Removenulllist.begin (), Removenulllist.end (), Comp); +Minnode = Removenulllist[removenulllist.size ()-1]; - Removenulllist.pop_back (); theTail->next =Minnode; *Tail = tail->Next; $             if(Minnode->next) {//If there's a list on the back, get in the heap again .Panax NotoginsengRemovenulllist.push_back (minnode->next); - make_heap (Removenulllist.begin (), Removenulllist.end (), Comp); the             } +         } A         returnHelper->Next; the     } +};

Leetcode oj:merge k Sorted Lists (merge K list)

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.