leetcode.23. 合并K个升序链表

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

 

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:

输入:lists = []
输出:[]
示例 3:

输入:lists = [[]]
输出:[]
 

提示:

k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/merge-k-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 
 
 
/**
 * 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;
        }
        ListNode dummyHead=new ListNode(0);
        ListNode curr=dummyHead;
         PriorityQueue<ListNode>pq=new  PriorityQueue<>(new Comparator<ListNode>(){
            @Override
            public int compare(ListNode o1,ListNode o2){
                return o1.val - o2.val;
            }
        });
        for(ListNode list:lists){
            if(list==null){
                continue;
            }
            pq.add(list);
        }
        while(!pq.isEmpty()){
            ListNode nextNode=pq.poll();
            curr.next =nextNode;
            curr=curr.next;
            if(nextNode.next!=null)pq.add(nextNode.next);
        }
         return dummyHead.next;
    }
}
 
 
 
 
第二种分治算法
 
/**
 * 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 merge(lists,0,lists.length-1);
    }
    public ListNode merge(ListNode[] lists,int low,int high)
    {
        if(high-low==0) return lists[low];
        else if(high-low==1) return mergeTwoLists(lists[low],lists[high]);
        else
        {
            int mid=(low+high)/2;
            ListNode tmp1=merge(lists,low,mid);
            ListNode tmp2=merge(lists,mid+1,high);
            return mergeTwoLists(tmp1,tmp2);
        }
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head=new ListNode();
        ListNode p=head;
        while(l1!=null&&l2!=null)
        {
            if(l1.val>l2.val)
            {
                p.next=l2;
                l2=l2.next;
                p=p.next;
            }
            else 
            {
                p.next=l1;
                l1=l1.next;
                p=p.next;
            }
        }
        if(l1!=null) p.next=l1;
        if(l2!=null) p.next=l2;
        return head.next;
    }
}
posted @   开源遗迹  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示