算法题_合并K个有序链表(通过优先队列实现)

一、题目分析

题目:将K个有序列表合并成为一条有序链表。
leetcode地址:https://leetcode.com/problems/merge-k-sorted-lists/
可以通过优先队列 来实现,PriorityQueue 称为小根堆:会自动的将最小值排在最前。
例如:此时堆中为:[3],加入一个元素2,变为[2,3];再加入一个元素5,变为[2,3,5]。弹出一个元素时,会把最小值2弹出,堆变为[3,5]。

二、代码实现

package com.lsh.day07;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author :LiuShihao
 * @date :Created in 2022/2/9 1:42 下午
 * @desc :合并K个升序列表:可以直接使用小根堆实现 优先队列
 *
 */
public class Code03_Q1 {
    public static class ListNode{
        public int value;
        public ListNode next;

        public ListNode(int v){
            this.value = v;
            next = null;
        }
    }
    //定义 ListNode 的比较器
    public static class MyComparator implements Comparator<ListNode> {

        @Override
        public int compare(ListNode o1, ListNode o2) {
            return o1.value-o2.value;
        }
    }

    /**
     * heap 意思是堆
     * @param lists 单链表集合 表示有K个链表
     * @return
     */
    public static ListNode mergeKLists(ListNode[] lists){
        if (lists == null){
            return null;
        }

        PriorityQueue<ListNode> heap = new PriorityQueue<>(new MyComparator());

        for (int i = 0; i < lists.length; i++) {
            //把每个链表的第一个元素(头节点)都加进 堆heap 去
            heap.add(lists[i]);
        }
        if (heap.isEmpty()){
            return null;
        }
        //弹出堆中最小的元素 作为新链表的头节点
        ListNode head = heap.poll();
        ListNode pre = head;
        if (pre.next != null){
            heap.add(pre.next);
        }
        while (!heap.isEmpty()){
            ListNode cur = heap.poll();
            pre.next = cur;
            pre = cur;
            if (pre.next != null){
                heap.add(pre.next);
            }
        }
        return head;
    }

 
    public static void printLinkedList(ListNode head){
        while (head != null){
            System.out.print(head.value + " -> ");
            head = head.next;
        }
        System.out.println();
    }
}

三、验证

在这里插入图片描述

   /**
     * 链表1:0 -> 4 -> 6 -> 7
     * 链表2:3 -> 5 -> 8 -> 8
     * 链表3:1 -> 3 -> 5 -> 10
     *
     * 合并后:0 -> 1 -> 3 -> 3 -> 4 -> 5 -> 5 -> 6 -> 7 -> 8 -> 8 -> 10 ->
     * @param args
     */
    public static void main(String[] args) {
        ListNode head1 = new ListNode(0);
        head1.next = new ListNode(4);
        head1.next.next = new ListNode(6);
        head1.next.next.next = new ListNode(7);

        ListNode head2 = new ListNode(3);
        head2.next = new ListNode(5);
        head2.next.next = new ListNode(8);
        head2.next.next.next = new ListNode(8);

        ListNode head3 = new ListNode(1);
        head3.next = new ListNode(3);
        head3.next.next = new ListNode(5);
        head3.next.next.next = new ListNode(10);

        ListNode[] lists = new ListNode[]{head1, head2, head3};

        ListNode head = mergeKLists(lists);
        printLinkedList(head);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Liu_Shihao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值