【LeetCode笔记】23.合并K个升序列表(Java、分治、链表)

冲的第一道hard,好耶!

题目描述

  • 这道题和前面的合并两个有序链表很有联系。直接调用了整个合并函数。
  • 可以看成我们已经有了足够优秀的“两条链表合并“的函数,然后考虑对K条链表如何进行合并分配。
  • 结构类似归并排序噢~
    在这里插入图片描述

解法 & 代码

  • 对K条链表,用一个merge不断二分;当merge只有两条时,进行twoList合并操作。只有一条时,直接返回当前链表。(这也解决了在二分时出现奇数的问题
  • 之前考虑过不用merge,直接for循环一次合并入一条,也能过,但是复杂度会到O((m+n)*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) {
        // 数组空的情况,和数组为null的情况
        if(lists == null || lists.length == 0){
            return null;
        }
        return merge(lists, 0, lists.length - 1);
    }

    // 递归进行合并操作,复杂度log(n)
    public ListNode merge(ListNode[] lists, int left, int right){
        // 数组就一条链表的情况 && 
        if (left == right)
            return lists[left];
        // 返回左、右两部分数组链表的两个结果,然后进行链表的两两合并twoList();
        return twoList(merge(lists,left, (right + left) / 2), 
        	merge(lists, (right + left) / 2 + 1, right));
    }

    // 放入两条链表,返回一条合并链表
    // 时间复杂度O(m+n)
    public ListNode twoList(ListNode l1, ListNode l2) {
        // 有链表为空的情况
        if(l1 == null)
            return l2;
        if(l2 == null)
            return l1;
        else if(l1.val < l2.val) {
            l1.next = twoList(l1.next,l2);
            return l1;
        }
        else {
            l2.next = twoList(l2.next,l1);
            return l2;
        }
    }
}
  • 时间复杂度O((m+n)*log(k)),也就是两链表合并的复杂度乘上K链表合并的复杂度
  • 空间复杂度O(1)

二刷

  • 这道题怎么样都是爆杀…hard友好题!
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length == 0) return null;
        return merge(lists, 0, lists.length - 1);
    }

    ListNode merge(ListNode[] lists, int left, int right) {
        if(left == right) return lists[left];
        if(left + 1 == right) return mergeTwoLists(lists[left], lists[right]);

        int mid = (left + right) / 2;
        return mergeTwoLists(merge(lists, left, mid), merge(lists, mid + 1, right));
    }

    ListNode mergeTwoLists(ListNode head1, ListNode head2) {
        if(head1 == null) return head2;
        if(head2 == null) return head1;
        if(head1.val < head2.val) {
            head1.next = mergeTwoLists(head1.next, head2);
            return head1;
        } else {
            head2.next =mergeTwoLists(head1, head2.next);
            return head2;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值