Leetcode 23. Merge k Sorted Lists

Using PriorityQueue (Minheap).

public class Solution {
    // Implement comparator for ListNode
    private class ListNodeComparator implements Comparator<ListNode> {
        public int compare(ListNode l1, ListNode l2) {
            return l1.val - l2.val;
        }
    }
    
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> queue = new PriorityQueue<>(new ListNodeComparator());
        
        ListNode dummy = new ListNode(0);
        ListNode tail = dummy;
        
        for (ListNode node:lists)
            if (node != null)
                queue.offer(node);
            
        while (!queue.isEmpty()){
            tail.next = queue.poll();
            tail = tail.next;
            
            if (tail.next != null)
                queue.offer(tail.next);
        }
        
        return dummy.next;
    }
}

Given a list array, for example, l1 l2 l3 l4 l5 l6 l7 l8.

Intuitive solution, merge l1 and l2 into new list l1', then merge l1' and l3 into l2', similarly, merge l2' and l4 into l3'.

Time complexity O(N^2).

Divide and conquer. Divide the original lists into small pieces (i.e. two lists), merge them, and then merge the bigger one, so on and so forth. Divide and conquer will take O(logN), merge will take O(N), 

total time complexity O(NlogN).

public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) {
            return null;
        }
        
        if (lists.length == 1) {
            return lists[0];
        }
        
        // intuitive solution, takes O(n)
        // merge the first two lists then merge the rest lists
        // ListNode ret = mergeTwoLists(lists[0], lists[1]);
        // for (int i=2; i<lists.length; i++) {
        //     ret = mergeTwoLists(ret, lists[i]);
        // }
        // divide and conquer, takes O(logn)
        return divideConquer(0, lists.length-1, lists);
    }
    
    private static ListNode divideConquer(int start, int end, ListNode[] lists) {
        if (start == end) {
            return lists[start];
        } else if (start < end) {
            int mid = (end-start)/2+start;
            ListNode l1 = divideConquer(start, mid, lists);
            ListNode l2 = divideConquer(mid+1, end, lists);
            return mergeTwoLists(l1, l2);
        }
        return null;
    }
    
    // recursively merge two lists
    private static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        
        if (list2 == null) {
            return list1;
        }
        
        // head of merged lists
        ListNode head;
        if (list1.val < list2.val) {
            head = list1;
            head.next = mergeTwoLists(list1.next, list2);
        } else {
            head = list2;
            head.next = mergeTwoLists(list1, list2.next);
        }
        
        return head;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值