栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

BM5 合并 k 个已排序的链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BM5 合并 k 个已排序的链表


解法一: 归并+递归,做这题之前先看BM4解发二比较好理解,时间复杂度nlog(n),空间复杂度O(n)

import java.util.*;

public class Solution {
    public ListNode mergeKLists(ArrayList lists) {

        if (lists == null || lists.size() == 0) {
            return null;
        }
        return partion(lists, 0, lists.size() - 1);
    }

    private ListNode partion(ArrayList lists, int left, int right) {
        if (left >= right) {
            return lists.get(left);
        }
        int mid = (left + right) / 2;
        ListNode node1 = partion(lists, left, mid);
        ListNode node2 = partion(lists, mid + 1, right);
        return merge(node1, node2);
    }

    private ListNode merge(ListNode node1, ListNode node2) {
        if (node1 == null) {
            return node2;
        }
        if (node2 == null) {
            return node1;
        }
        ListNode pre = null;
        if (node1.val < node2.val) {
            pre = node1;
            pre.next = merge(node1.next, node2);
        } else {
            pre = node2;
            pre.next = merge(node1, node2.next);
        }
        return pre;
    }
}

方法二:归并+循环,时间复杂度O(nlog(n)),空间复杂度O(n)

import java.util.*;

public class Solution {
    public ListNode mergeKLists(ArrayList lists) {

        if (lists == null || lists.size() == 0) {
            return null;
        }
        return partion(lists, 0, lists.size() - 1);
    }

    private ListNode partion(ArrayList lists, int left, int right) {
        if (left >= right) {
            return lists.get(left);
        }
        int mid = (left + right) / 2;
        ListNode node1 = partion(lists, left, mid);
        ListNode node2 = partion(lists, mid + 1, right);
        return merge(node1, node2);
    }

    private ListNode merge(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode cur = new ListNode(-1);
        ListNode pre = cur;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                cur.next = list1;
                list1 = list1.next;
                cur = cur.next;
            } else if (list1.val > list2.val) {
                cur.next = list2;
                list2 = list2.next;
                cur = cur.next;
            } else {
                cur.next = list1;
                list1 = list1.next;
                cur = cur.next;

                cur.next = list2;
                list2 = list2.next;
                cur = cur.next;
            }
        }
        while (list1 != null) {
            cur.next = list1;
            list1 = list1.next;
            cur = cur.next;
        }
        while (list2 != null) {
            cur.next = list2;
            list2 = list2.next;
            cur = cur.next;
        }
        return pre.next;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776677.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号