Leetcode 23. Merge K Sorted Lists (merging K sorted list) thinking and method of solving problems

Source: Internet
Author: User

Merge k Sorted Lists

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


Idea: The problem is from the merger of two sort lists evolved from the beginning, the idea is relatively simple, like the largest public prefix, one by one, but finally time out, so immediately realize that the topic is to use the merger and division of the method, so the code re-write.

Code one (timeout not over):

/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {val = x;}}} */public class Soluti        On {public ListNode mergeklists (listnode[] lists) {int len = lists.length;        if (len = = 0) {return null;        } if (len = = 1) {return lists[0];        } ListNode head = Lists[0];        for (int i = 0; i < len; i++) {head = Mergetwolists (Head,lists[i]);    } return head;  } public ListNode mergetwolists (listnode L1, ListNode L2) {if (L1 = = NULL | | l2 = null) return L1 = = Null?        L2:L1;        ListNode head = new ListNode (0);//define a head node ListNode p = head;                ListNode temp = null;                while (L1 = null && L2! = null) {if (L1.val > l2.val) {temp = l2;//Save the current L1 or L2 with a temp                L2 = L2.NEXT;//L1 or L2 pointer moves back 1 bits}else{temp = L1; L1 = L1. Next;            }//Exchange Data P.next = temp;        p = p.next;        }//temp the one that is not empty (and possibly all empty) temp = L1 = = null? L2:L1;    P.next = temp;//will all the remaining links (the method above is too verbose) return head.next; }}
Code two (pass):

/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {val = x;}}} */public class Soluti        On {public ListNode mergeklists (listnode[] lists) {int len = lists.length;        If the length is <=1 (len = = 0) {return null;        } if (len = = 1) {return lists[0];        } listnode[] ln = lists;        while (Len > 2) {//greater than or equal to 2 divide ln = merge (ln);//Call function len = ln.length;//update len}//last two merge        Ln[0] = mergetwolists (ln[0],ln[1]);    return ln[0];        }//To lists 22 merge public listnode[] Merge (listnode[] lists) {int len = lists.length;        22 merge listnode[] arr;        The length is even if ((len & 1) = = 0) {arr = new LISTNODE[LEN/2];            }else{//length is odd arr = new LISTNODE[LEN/2 + 1]; ARR[LEN/2] = lists[len-1];//The last 1 bits do not participate in the merge}//22 merge implementation; I < len-1; not i<len; or error for (int i= 0; i < len-1;        i = i+2) {ListNode a = lists[i];            ListNode B = lists[i+1];    ARR[I/2] = mergetwolists (lists[i],lists[i+1]);//Call 22 Merge Method} return arr; } public ListNode mergetwolists (listnode L1, ListNode L2) {if (L1 = = NULL | | l2 = NULL) return L 1 = = null?        L2:L1;        ListNode head = new ListNode (0);//define a head node ListNode p = head;                ListNode temp = null;                while (L1 = null && L2! = null) {if (L1.val > l2.val) {temp = l2;//Save the current L1 or L2 with a temp                L2 = L2.NEXT;//L1 or L2 pointer moves back 1 bits}else{temp = L1;            L1 = L1.next;            }//Exchange Data P.next = temp;        p = p.next;        }//temp the one that is not empty (and possibly all empty) temp = L1 = = null? L2:L1;    P.next = temp;//will all the remaining links (the method above is too verbose) return head.next; }}



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

Leetcode 23. Merge K Sorted Lists (merging K sorted list) thinking and method of solving problems

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.