The merge K Sorted Lists merges K ordered linked lists

Source: Internet
Author: User

This problem is a 21-question merger of 2 sequential linked list upgrade version, see a lot of problem-solving ideas:

A: direct violence to unlock, all put into a heap, and then spit out in turn;

B: Using 21 of the algorithm, the loop to do 22 merges, so that the results, but the complexity of the time is a bit bad;

C: Using the idea of merging and sorting, divide and conquer, in fact, the use of recursion, sacrifice space, improve time efficiency;

The problem is: After reading many answers, we find that the given data type is LIST<LISTNODE>/ARRAYLIST<LISTNODE>, and then, now the system is updated, the data type given is listnode[] lists So, I am now re-designing this topic absolutely pleasing to all!

I'll stick to the original program I found in the discussion:

  PublicListNode mergeklists (listnode[] lists) {if(Lists = =NULL|| lists.length== 0)        return NULL; if(lists.length==1){        returnLists[0]; }     returnMergeklists (lists,0,lists.length-1); }  PublicListNode mergeklists (listnode[] lists,intLointhi) {          intm=lo+ (Hi-lo)/2; ListNode Left=NULL; if(m<=lo) { Left=Lists[m]; }Else{ Left=mergeklists (lists,lo,m); } ListNode Right=NULL; if(m+1>=hi) { Right=Lists[hi]; }Else{ Right=mergeklists (lists,m+1, HI); }         returnmerge2list (left,right);}PrivateListNode merge2list (listnode list1,listnode list2) {ListNode dummy=NewListNode (0); ListNode cur=dummy;  while(true){        if(list1==NULL&&list2==NULL){             Break; }Else if(list1==NULL&& list2!=NULL) {Cur.next=List2;  Break; }        Else if(list1!=NULL&& list2==NULL) {Cur.next=List1;  Break; }Else if(list1.val<list2.val) {Cur.next=List1; List1=List1.next; }Else{Cur.next=List2; List2=List2.next; } cur=Cur.next; }    returnDummy.next;}

Very clever, using bridging, the problem is set to merge the sort or the dichotomy of the classic routines above. I thought for a long time what to do. Of course, the memory of time is worth it. Hopefully this approach will be useful in future development.

In addition, I have optimized his division. Because, considering the division rounding problem, I found that whether it is the " odd number array or an even number array " Division of the end, or the bottom of the 2 cases: (A,a) (a,a+1) Two cases, other cases can be divided into these two situations. So a separate design for both cases is quite appropriate for the return!

 Public classSolution { PublicListNode merge2lists (listnode L1, ListNode L2) {ListNode head=NewListNode (-1); ListNode Index=Head;  while(L1! =NULL&& L2! =NULL){            if(L1.val <l2.val) {Index.next=L1; L1=L1.next; Index=Index.next; }            Else{Index.next=L2; L2=L2.next; Index=Index.next; }        }        if(L1! =NULL) Index.next=L1; ElseIndex.next=L2; returnHead.next; }     PublicListNode mergeklists (listnode[] lists,intLointhi) {        if(lo = hi)returnLists[lo]; intMid = lo + (Hi-lo)/2; if(hi = = lo + 1)           {               returnmerge2lists (Lists[lo],lists[hi]); } ListNode Left=mergeklists (Lists, lo, mid); ListNode Right= mergeklists (lists, mid+1, HI); returnmerge2lists (left,right); }     PublicListNode mergeklists (listnode[] lists) {if(Lists = =NULL|| Lists.length = = 0)return NULL; if(Lists.length = = 1)returnLists[0]; returnMergeklists (lists, 0, lists.length-1); }}

The merge K Sorted Lists merges K ordered linked 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.