[LeetCode]23. 合并K个升序链表(java实现)小根堆

1. 题目

在这里插入图片描述

2. 读题(需要重点注意的东西)

思路1(归并法):合并n个升序数组,首先便可以想到使用归并法。
思路2(小根堆)推荐:数组中存储的是各个链表的头结点,并且各个链表已经按升序排列了,那么将所有的头结点存储在小根堆中重载运算符使得小根堆按各个链表头结点的值进行排序,每次输出最小的节点(heap.poll即可,时间复杂度为O(1))到新的链表中,直至所有链表都为空即可。

3. 解法

--------------------------解法1(归并法)-----------------------------

/**
 * 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;
        if(lists.length == 1)
            return lists[0];
        if(lists.length == 2){
           return mergeTwoLists(lists[0],lists[1]);
        }

        int mid = lists.length/2;
        ListNode[] l1 = new ListNode[mid];
        for(int i = 0; i < mid; i++){
            l1[i] = lists[i];
        }

        ListNode[] l2 = new ListNode[lists.length-mid];
        for(int i = mid,j=0; i < lists.length; i++,j++){
            l2[j] = lists[i];
        }

        return mergeTwoLists(mergeKLists(l1),mergeKLists(l2));

    }
    // 归并排序
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;

        ListNode head = null;
        if (l1.val <= l2.val){
            head = l1;
            head.next = mergeTwoLists(l1.next, l2);
        } else {
            head = l2;
            head.next = mergeTwoLists(l1, l2.next);
        }
        return head;
    }
}

--------------------------解法2(小根堆,推荐)-----------------------------

class Solution {
    
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> heap=new PriorityQueue<ListNode>(new Comparator<ListNode>() {
		    public int compare(ListNode o1,ListNode o2) {
			    return o1.val-o2.val;
		    }
	    });
		// 上述小根堆也可按如下格式定义
		// PriorityQueue<ListNode> heap = new PriorityQueue<>((a, b) -> a.val - b.val);
        ListNode dummy = new ListNode(-1);
        ListNode tail = dummy;
        for (int i = 0; i < lists.length; i++){
            if (lists[i] != null) heap.add(lists[i]);
        }
        while (!heap.isEmpty()){
            ListNode t = heap.poll();
            tail = tail.next = t;
            if (t.next != null) heap.add(t.next); 
        }
        return dummy.next;
    }
}

4. 可能有帮助的前置习题

5. 所用到的数据结构与算法思想

6. 总结

// 归并排序
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null) return l2;
    if (l2 == null) return l1;

    ListNode head = null;
    if (l1.val <= l2.val){
        head = l1;
        head.next = mergeTwoLists(l1.next, l2);
    } else {
        head = l2;
        head.next = mergeTwoLists(l1, l2.next);
    }
    return head;
}
// ---------------------------------------------------------------------------
// 小根堆的定义方法
// 方法一:
PriorityQueue<ListNode> heap=new PriorityQueue<ListNode>(new Comparator<ListNode>() {
		    public int compare(ListNode o1,ListNode o2) {
			    return o1.val-o2.val;
		    }
	    });
// 方法二:
// PriorityQueue<ListNode> heap = new PriorityQueue<>((a, b) -> a.val - b.val);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cloudeeeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值