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