#23 Merge k Sorted Lists

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/merge-k-sorted-lists/


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


/** * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *}; */void percolateup (struct listnode** lists, int i) {//Pre I elements are already ordered, upper filter i+1 element struct listnode* tmp = lists[i];while (i > 0 && lists[(i-1)/2]->val > Tmp->val) {//element to the root node path all nodes larger than that element are moved down lists[i] = lists[(i-1)/2];i = (i-1 )/2;} Lists[i] = tmp;} int buildheap (struct listnode** lists, int k) {int i, size = 0;for (i = 0; i < K; ++i)//insert non-empty element into heap if (Lists[i]) {lists [Size] = Lists[i];p ercolateup (lists, size); ++size;} return size;} Only the top elements of the heap are not in order to be filtered; that is, the smaller son is constantly moved to the hole until the younger one is not less than the heap top element void Percolatedown (struct listnode** lists, int size) {struct listnode* TMP = Lists[0];int i = 0, Smallchild = 2 * i + 1;//first record left son for smaller son, if right son smaller, update if (Smallchild + 1 < size && Lists[sma Llchild]->val > Lists[smallchild + 1]->val) ++smallchild;while (Smallchild < size && lists[ Smallchild]->val < Tmp->val) {Lists[i] = Lists[smallchild];i = Smallchild;smallChild = 2 * i + 1;if (smallchild + 1 < size && Lists[smallchild]->val > lists[smallchild + 1]->val) ++s Mallchild;} Lists[i] = tmp;} Heap implementation Priority queue, the first element of the K list is built heap, each popup heap top element;//delete heap top element after refactoring requires log (k) operation, total time complexity n*log (k) struct listnode* mergeklists (struct listnode** Lists, int listssize) {if (lists = = NULL | | listssize <= 0) return null;if (listssize = 1) return Lists[0];int Size;stru CT listnode* head = (struct ListNode *) malloc (sizeof (struct listnode));//Dummy node struct listnode* p = head;head->next = NULL ;//must not be small, otherwise all the list is empty can not return nullsize = buildheap (lists, listssize),//will lists sorted by the heap, and return the initial heap size, that is, the number of non-empty list while (size) {///As long as the heap is not empty, Each pop-up heap top element, update heap P->next = lists[0];p = p->next;lists[0] = lists[0]->next;//Update heap top element if (lists[0] = null) {//if the top element of the heap is empty, Move the trailing element to the top of the heap lists[0] = lists[size-1];--size;} Percolatedown (lists, size);//The top element of the filter heap keeps the heap order}p = Head->next;free (head); return p;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

#23 Merge k Sorted Lists

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.