23 合并K个升序链表

leetcode 23 合并K个升序链表

在这里插入图片描述

分治归并思路

思路类似于数组的归并排序,两两组合,每次由两个链表合并

/**
 * 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) {
        return merge(lists,0,lists.length-1);
    }
    public ListNode merge(ListNode[] lists,int L,int R){
        if(L>R){
            return null;
        }
        if(L==R){
            return lists[L];
        }
        // int mid = R-(R-L)/2;
        int mid = (L + R) >> 1;
        return mergeTwoList(merge(lists,L,mid),merge(lists,mid+1,R));
    }
    public ListNode mergeTwoList(ListNode list1,ListNode list2){
        if(list1==null | list2==null){
            return list1==null?list2 : list1;
        }
        ListNode head = new ListNode(0);
        ListNode tail = head;
        ListNode aPtr = list1;
        ListNode bPtr = list2;
        while(aPtr != null && bPtr != null){
            if(aPtr.val < bPtr.val){
                tail.next = aPtr;
                aPtr = aPtr.next;
            }else{
                tail.next = bPtr;
                bPtr = bPtr.next;
            }
            tail = tail.next;
        }
        tail.next = aPtr != null? aPtr : bPtr;
        return head.next;
    }
}

优先队列思路

优先队列中存n个队列的头部,让优先队列来进行排序,降低复杂度(注意:需要重写优先队列的Compare逻辑)

/**
 * 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 {
    class Comp implements Comparable<Comp> {
        Comp(ListNode node,int val){
            this.node = node;
            this.val = val;
        }
        ListNode node;
        int val;
        public int compareTo(Comp comp2){
            return this.val - comp2.val;
        }
    }
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<Comp> queue = new PriorityQueue<Comp>();
        for(int i = 0;i<lists.length;i++){
            if(lists[i] != null){
                queue.offer(new Comp(lists[i],lists[i].val));
            }
        }
        ListNode head = new ListNode(0);
        ListNode tail = head;
        while(!queue.isEmpty()){
            Comp c = queue.poll();
            tail.next = c.node;
            tail = tail.next;
            if(c.node.next != null){
                queue.offer(new Comp(c.node.next,c.node.next.val));
            }
        }
        return head.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值