# 23. Merge k Sorted Lists

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mij2gxTMBg1-zZLaM1b%2F-MijOB6tapGhh-aZKIWu%2Fimage.png?alt=media\&token=8354a6bd-5339-4d91-95cb-74012c6d6a1f)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mij2gxTMBg1-zZLaM1b%2F-MijODfjVGE9NCHfFpQD%2Fimage.png?alt=media\&token=8ba9ca36-9f13-46dc-95e0-9b71b9f8636d)

### solution1 - use divide and conquer

divide and conquer => logk, k is lists\[] length, split to 2 part

merge O(N), N is all nodes in two lists

&#x20;, so O(Nlogk)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mij2gxTMBg1-zZLaM1b%2F-MijNJTaEY1KxZNusuP9%2Fimage.png?alt=media\&token=fed9c1af-47b6-469c-af3a-8e3206527b5a)



**notice**, in this case, **should return null => . output: \[]**&#x20;

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mij2gxTMBg1-zZLaM1b%2F-MijMhFtLOzuM47TzWy9%2Fimage.png?alt=media\&token=b94337e2-e517-42d8-80a7-9f8e1a7fdba9)

```java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null; // notice this!
        
        return partition(lists, 0, lists.length - 1);
    }
    
    private ListNode partition(ListNode[] lists, int s, int e) {
        if (s == e) {
            return lists[s]; // notice this!
        } else if (s < e) {
            int mid = (s+e)/2;
            ListNode l1 = partition(lists, s, mid);
            ListNode l2 = partition(lists, mid+1, e);
            return merge(l1, l2);
        } else {
            return null; // notice this!
        }
    }
    
    // this part is leetcode 21. Merge Two Sorted Lists
    private ListNode merge(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1); // notice this should new a node
        ListNode pre = head;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                pre.next = l1;
                l1 = l1.next;
            } else {
                pre.next = l2;
                l2 = l2.next;
            }
            pre = pre.next;
        }
        pre.next = (l1 == null) ? l2 : l1;
        
        return head.next;
    }
}
```

假設只有兩個數, index 0, 1

所以 mid = 0+1)/2 = 0

l1 = partition(lists, 0,0) = ( s == e return lists\[s] ) =  list\[0]

l2 = partition(lists, 0+1,1) = ( s == e return lists\[s] ) =  list\[1]

return merge(list\[0], list\[1])&#x20;

```java
    private ListNode partition(ListNode[] lists, int s, int e) {
        if (s == e) {
            return lists[s]; // notice this!
        } else if (s < e) {
            int mid = (s+e)/2;
            ListNode l1 = partition(lists, s, mid);
            ListNode l2 = partition(lists, mid+1, e);
            return merge(l1, l2);
        } else {
            return null; // notice this!
        }
    }
```





### solution2 - use priorityQueue

easier solution!

```java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        
        for (ListNode list : lists) {
            if (list != null) {
                queue.add(list);
            }
        }
        while (!queue.isEmpty()) {
            cur.next = queue.poll();
            cur = cur.next;
            if (cur.next != null) {
                queue.add(cur.next);
            }
        }
        return dummy.next;
    }
}
```
