Merge k Sorted Lists leetcode java

愛做飯的小瑩子發表於2014-07-26

題目:

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

 

題解:

Merge k sorted linked list就是merge 2 sorted linked list的變形題。

而且我們很自然的就想到了經典的Merge Sort,只不過那個是對陣列進行sort。而不同的地方,僅僅是Merge兩個list的操作不同。

 

這裡來複習一下Merge Sort(對於陣列操作),參考Wikipedia:

歸併操作(merge),也叫歸併演算法,指的是將兩個已經排序的序列合併成一個序列的操作。歸併排序演算法依賴歸併操作。

歸併操作的過程如下:

  1. 申請空間,使其大小為兩個已經排序序列之和,該空間用來存放合併後的序列
  2. 設定兩個指標,最初位置分別為兩個已經排序序列的起始位置
  3. 比較兩個指標所指向的元素,選擇相對小的元素放入到合併空間,並移動指標到下一位置
  4. 重複步驟3直到某一指標到達序列尾
  5. 將另一序列剩下的所有元素直接複製到合併序列尾
最差時間複雜度 \Theta(n\log n)
最優時間複雜度 \Theta(n)
平均時間複雜度 \Theta(n\log n)
最差空間複雜度 \Theta(n)

 Merge Sort 對陣列操作的Java程式碼為:

 1 public int[] mergeSort(int[] arr){
 2     if(arr.length<2||arr == null)
 3         return arr;
 4     
 5     MSort(arr,0,arr.length-1);
 6 }
 7 
 8 public int[] MSort(int[] arr, int low, int high){
 9         if(low < high){
10                 int mid = (low+high)/2;
11                 int[] left = MSort(arr,low,mid);
12                 int[] right = MSort(arr,mid+1,high);
13                 return mergeTwoList(left,right);
14           }  
15 }
16 
17 
18 public int[] mergeTwoList(int[] A, int[] B) {
19     int[] C = new int[A.length + B.length];
20     int k = 0;
21     int i = 0;
22     int j = 0;
23     while(i < A.length && j < B.length) {
24         if (A[i] < B[j])
25             C[k++] = A[i++];
26         else
27             C[k++] = B[j++];
28     }
29     while (i < A.length) 
30         C[k++] = A[i++];
31     while (j < B.length) 
32         C[k++] = B[j++];
33     return C;
34 }

 下面就是這道題的解法,跟上面的方法一樣一樣的,就是在mergeTwoList時候是對linkedlist做,套用Merge 2 sorted list解法即可,程式碼如下:

 1     public ListNode mergeKLists(ArrayList<ListNode> lists) {  
 2         if(lists==null || lists.size()==0)  
 3             return null;  
 4         return MSort(lists,0,lists.size()-1);  
 5     }  
 6     
 7     public ListNode MSort(ArrayList<ListNode> lists, int low, int high){  
 8         if(low < high){  
 9             int mid = (low+high)/2;
10             ListNode leftlist = MSort(lists,low,mid);
11             ListNode rightlist = MSort(lists,mid+1,high);
12             return mergeTwoLists(leftlist,rightlist);  
13         }  
14         return lists.get(low);  
15     }
16 
17 
18     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
19             if(l1==null)
20                 return l2;
21             if(l2==null)
22                 return l1;
23                 
24             ListNode l3;
25             if(l1.val<l2.val){
26                 l3 = l1;
27                 l1 = l1.next;
28             }else{
29                 l3 = l2;
30                 l2 = l2.next;
31             }
32             
33             ListNode fakehead = new ListNode(-1);
34             fakehead.next = l3;
35             while(l1!=null&&l2!=null){
36                 if(l1.val<l2.val){
37                     l3.next = l1;
38                     l3 = l3.next;
39                     l1 = l1.next;
40                 }else{
41                     l3.next = l2;
42                     l3 = l3.next;
43                     l2 = l2.next;
44                 }
45             }
46             
47             if(l1!=null)
48                 l3.next = l1;
49             if(l2!=null)
50                 l3.next = l2;
51             return fakehead.next;
52         }

 更多Mergesort的講法請參考:http://www.cs.princeton.edu/courses/archive/spr07/cos226/lectures/04MergeQuick.pdf

 講的挺好的

相關文章