Merge K Sorted Lists, K-Way merge

Source: Internet
Author: User
Tags comparable

Importjava.util.Arrays;Importjava.util.List;ImportJava.util.PriorityQueue;/*class listnode{ListNode next;    int Val;    ListNode (int x) {val = x; }}*///K-Way merger problem Public classmergksortedlists {//Two-way merge, this algorithm time complexity O (2n)     PublicListNode mergetwolists (listnode L1, ListNode L2) {ListNode dumy=NewListNode (0); ListNode Head=dumy;  while(L1! =NULL&& L2! =NULL) {            if(L1.val <l2.val) {Dumy.next=L1; L1=L1.next; } Else{Dumy.next=L2; L2=L2.next; } dumy=Dumy.next; }        if(L1 = =NULL) {Dumy.next=L2; }        if(L2 = =NULL) {Dumy.next=L1; }        returnHead.next; }    //using the two-way merge for K-way merge, Time complexity O (2kn), Leetcode shows the timing limits exceeds     PublicListNode mergeKLists1 (listnode[] lists) {if(Lists.length = = 0)        {            return NULL; } ListNode head= Lists[0];  for(inti = 1; i < lists.length; i + +) {Head=mergetwolists (Head, lists[i]); }        returnHead; }        //very interesting ideas on the Internet. Think of ListNode as an integer, which is exactly the two-way merge sort of the array, but the element inside the array is the node//This method is obviously time-out, the idea, seemingly two-way merge, in fact, is not, the granularity is not the same. The time complexity of this algorithm is also not O (NLGN)     PublicListNode mergerKlists2 (listnode[] lists) {//top-down two-way merge, recursive        if(Lists.length = = 0)        {            return NULL; } List<ListNode> A =arrays.aslist (lists); returnHelper (a); }     PublicListNode Helper (list<listnode>lists) {        if(lists.size () = = 1)        {            returnLists.get (0); }        intLen =lists.size (); intMID = Len/2; ListNode L1= Helper (lists.sublist (0, mid)); ListNode L2=Helper (Lists.sublist (Mid, Len)); returnmergetwolists (L1, L2); }            //using the Priorityqueue features, it is also ingenious, and the AC//The idea of the algorithm is to compare the first element of all k arrays, find the smallest one, and then take it out. //we take the next element in the array where the smallest element is located, and then repeat the previous procedure to find the smallest one. This loops in turn until all the elements are found.      PublicListNode mergeKLists3 (listnode[] lists) {if(Lists.length = = 0)            return NULL; //since ListNode does not implement the comparable interface, we must customize the collation to implement the comparator interface, comparator is a functional interface//The comparable interface is within the method, while the comparator interface is outside the method of attention difference between the twopriorityqueue<listnode> queue =NewPriorityqueue<> (O1, O2){listnode L1=(ListNode) O1; ListNode L2=(ListNode) O2; returnL1.val > L2.val? 1:l1.val < L2.val? -1:0;        }); ListNode Head=NewListNode (0); ListNode P=Head;  for(ListNode list:lists) {queue.offer (list); }         while(!Queue.isempty ()) {ListNode n=Queue.poll (); P.next=N; P=P.next; if(n.next!=NULL) {queue.offer (n.next); }        }        returnHead.next; }}

Heap sorting algorithm Follow-up supplement ....

Merge K Sorted Lists, K-Way merge

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.