23. Merge k Sorted Lists

本文提供两种不同复杂度的K路归并链表算法实现:一种为O(NK)复杂度,通过逐个比较各链表头部节点来确定最小值;另一种为O(NlogK)复杂度,利用优先队列提高查找效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.


K路合并,O(NK)版本,每路一个指针,每轮找到最小值,推进相应位置指针。

类似于

 

373. Find K Pairs with Smallest Sums

http://blog.youkuaiyun.com/wdlsjdl2/article/details/51913760


这里懒得写了,贴一个

http://blog.youkuaiyun.com/beiyetengqing/article/details/7685593



public class Solution {  
    public ListNode mergeKLists(ArrayList<ListNode> lists) {  
        ListNode head = null;  
        ListNode current = null;  
          
        boolean isAllNull = false;  
        while (!isAllNull) {  
            isAllNull = true;  
            ListNode min = null;  
            int position = -1;  
            for (int i = 0; i < lists.size(); i++) {  
                if (min == null && lists.get(i) != null) {  
                    min = lists.get(i);  
                    isAllNull = false;  
                    position = i;  
                } else if (lists.get(i) != null){  
                    isAllNull = false;  
                    if (min.val > lists.get(i).val) {  
                        min = lists.get(i);  
                        position = i;  
                    }  
                }  
            }  
            if (head == null) {  
                head = min;  
                current = min;  
            } else {  
                current.next = min;  
                current = current.next;  
            }  
            if (position != -1) {  
                lists.set(position, lists.get(position).next);  
            }  
        }  
        return head;  
    }  
}  




--------------------------------------------------------------------------------------------------------------------------------------

O(NlogK)版本的,使用优先队列库函数

https://discuss.leetcode.com/topic/2780/a-java-solution-based-on-priority-queue


public class Solution {
    public ListNode mergeKLists(List<ListNode> lists) {
        if (lists==null||lists.size()==0) return null;
        
        PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.size(),new Comparator<ListNode>(){
            @Override
            public int compare(ListNode o1,ListNode o2){
                if (o1.val<o2.val)
                    return -1;
                else if (o1.val==o2.val)
                    return 0;
                else 
                    return 1;
            }
        });
        
        ListNode dummy = new ListNode(0);
        ListNode tail=dummy;
        
        for (ListNode node:lists)
            if (node!=null)
                queue.add(node);
            
        while (!queue.isEmpty()){
            tail.next=queue.poll();
            tail=tail.next;
            
            if (tail.next!=null)
                queue.add(tail.next);
        }
        return dummy.next;
    }
}




------------------------------------------------------------------------------------------------------------------------------

O(NlogK)版本的,参考《algroithms》第四版,代码非本题,只是阐述类似思想。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值