Leetcode #23 Merge k Sorted Lists (H)

Source: Internet
Author: User

[Problem]

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

[Analysis]

As soon as this question comes up, you'll think of the merge two Sorted Lists that I've done before. But if you simply keep the new list merge into the result, the complexity is very high, because you can think about it and find that the elements that have been mixed in the results in this process need to be reordered, and this part of the effort is optimized by using reasonable techniques. What you need here is the heap data structure. It's easier to think about the steps after the heap, just put all the list head in the heap, take a link to the top one at a time, and put its next in the heap (if it exists) until the heap is empty and all the elements are accessed once. Both the access element and the complexity of the link are O (1), and the complexity of the heap to the new element is O (log (k)), so the complexity of the entire algorithm is O (log (k) * N), n is the sum of the number of elements in all lists.

* Note that there are many null case needs to be judged.

* The heap implementation in Java is priorityqueue.

[Solution]

ImportJava.util.Comparator;ImportJava.util.PriorityQueue; Public classSolution { PublicListNode mergeklists (listnode[] lists) {intSize =lists.length; if(Size = = 0) {            return NULL; }                if(Size = = 1) {            returnLists[0]; } priorityqueue<ListNode> heap =NewPriorityqueue<listnode>(            2,             NewComparator<listnode>() {@Override Public intCompare (ListNode node1, ListNode node2) {returnNode1.val-Node2.val;                }            }        );  for(ListNode node:lists) {if(Node! =NULL) {Heap.add (node); }} ListNode head=NULL; ListNode cur=NULL;  while(!Heap.isempty ()) {ListNode node=Heap.poll (); if(Node.next! =NULL) {heap.add (node.next); }                        if(Head = =NULL) {Head=node; Cur=node; } Else{Cur.next=node; Cur=Cur.next; }        }                returnHead; }    }

Leetcode #23 Merge k Sorted Lists (H)

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.