---
# System prepended metadata

title: Merge k Sorted Lists
tags: [Hard]

---

# Merge k Sorted Lists
###### tags: `Hard`
>question : https://leetcode.com/explore/interview/card/top-interview-questions-hard/117/linked-list/839/

>reference : 

>related problem : 
## My Solution
***merge sort***
```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.length == 0) return null;
        return mergeKLists(lists, 0, lists.length - 1);
    }
    
    private ListNode mergeKLists(ListNode[] lists, int start, int end) {
        if(start == end)
            return lists[start];
        int mid = start + (end - start) / 2;
        ListNode node1 = mergeKLists(lists, start, mid);
        ListNode node2 = mergeKLists(lists, mid + 1, end);
        ListNode ans = new ListNode();
        ListNode tmp = ans;
        while(node1 != null && node2 != null) {
            if(node1.val < node2.val) {
                ans.next = node1;
                node1 = node1.next;
                ans = ans.next;
            }
            else {
                ans.next = node2;
                node2 = node2.next;
                ans = ans.next;
            }
        }
        if(node1 != null)
            ans.next = node1;
        else 
            ans.next = node2;
        return tmp.next;
    }
}
```
## Other Solution