[LintCode] Merge K Sorted Lists [DC/Heap]

Problem

Merge k sorted linked lists and return it as one sorted list.

Analyze and describe its complexity.

Example

Given lists:

[
  2->4->null,
  null,
  -1->null
],

return -1->2->4->null.

Note

分治做法中,merge()函数依然是将链表结点两两进行比较,然后在sort()函数中迭代merge两个二分后sort()的结果。PriorityQueue更为简洁。

Solution

Divide & Conquer

public class Solution {
    public ListNode mergeKLists(List<ListNode> lists) {  
        if (lists == null || lists.size() == 0) return null;
        return sort(lists, 0, lists.size()-1);
    }
    public ListNode sort(List<ListNode> lists, int start, int end) {
        if (start < end) {
            int mid = (start+end)/2;
            return merge(sort(lists, start, mid), sort(lists, mid+1, end));
        }
        return lists.get(start);
    }
    public ListNode merge(ListNode n1, ListNode n2) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (n1 != null && n2 != null) {
            if (n1.val < n2.val) {
                cur.next = n1;
                n1 = n1.next;
            }
            else {
                cur.next = n2;
                n2 = n2.next;
            }
            cur = cur.next;
        }
        if (n1 != null) cur.next = n1;
        else cur.next = n2;
        return head.next;
    }
}

Priority Queue

Edited: 2018.3
public class Solution {
    public ListNode mergeKLists(List<ListNode> lists) {  
        if (lists == null || lists.size() == 0) return null;
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode> (1, new Comparator<ListNode> (){
            public int compare(ListNode n1, ListNode n2) {
                return n1.val - n2.val;
            }
        });
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i) != null) pq.offer(lists.get(i));
        }
        while (!pq.isEmpty()) {
            head.next = pq.poll();
            head = head.next;
            //put the rest ListNode back to pq
            if (head.next != null) pq.offer(head.next);
        }
        return dummy.next;
    }
}
PriorityQueue Java 8
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        PriorityQueue<ListNode> heap = new PriorityQueue<>((n1, n2) -> n1.val-n2.val);
        for (ListNode n: lists) {
            if (n != null) heap.offer(n);
        }
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (!heap.isEmpty()) {
            cur.next = heap.poll();
            cur = cur.next;
            if (cur.next != null) heap.offer(cur.next);
        }
        return head.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值