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

解法1:

  采用递归的方法,不管合并几个,归根到底还是需要两两合并。

  首先想到的是前两个先合并,然后再跟第三个合并,然后第四个。。。。但是这种做法效率不高。

  换个思路,采用分治法,对数量超过2的任务进行拆分,直到最后只有一个或两个链表再进行合并。代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null; } else if (lists.length == 1) {
return lists[0]; } else {
ListNode res = new ListNode(0);
ListNode last = res; int mid = lists.length / 2;
ListNode one = mergeKLists(Arrays.copyOfRange(lists, 0, mid));
ListNode two = mergeKLists(Arrays.copyOfRange(lists, mid, lists.length)); while (one != null && two != null) {
if (one.val < two.val) {
last.next = one;
one = one.next;
} else {
last.next = two;
two = two.next;
}
last = last.next;
} last.next = one != null ? one : two;
return res.next;
} }
}

或者:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
} int n = lists.length;
while (n > 1) {
int k = (n + 1) / 2;
for (int i = 0; i < n / 2; i++) {
lists[i] = mergeTwoLists(lists[i], lists[i + k]);
}
n = k;
}
return lists[0];
} public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode head = new ListNode(0);
ListNode last = head; while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
last.next = list1;
list1 = list1.next;
} else {
last.next = list2;
list2 = list2.next;
}
last = last.next;
} last.next = list1 != null ? list1 : list2;
return head.next;
}
}

解法2:

  采用小根堆的方法,先将k个链表的首节点加入堆中,每次会自动输出最小的节点,将该节点加入到最终结果的链表中,然后将其下一个元素(如果不为null)加入堆中,直到最终堆为空,即输出了全部元素。代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>() {
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1; // 这样操作的原因是,确保堆顶(最小节点)不会为null
} else if (right == null) {
return -1; // 这样操作的原因是,确保堆顶(最小节点)不会为null
} // 因为下面操作中确保堆中不会有null,所以这两个判断不要亦可
return left.val - right.val;
}
}; public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
} Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.length, ListNodeComparator);
for (ListNode node : lists) {
if (node != null) {
heap.add(node);
}
} ListNode dummy = new ListNode(0);
ListNode last = dummy;
while (!heap.isEmpty()) {
last.next = heap.poll();
last = last.next;
if (last.next != null) {
heap.add(last.next);
}
}
return dummy.next;
}
}

[LeetCode] 23. Merge k Sorted Lists ☆☆的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

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

  2. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

  3. [leetcode 23]Merge k Sorted Lists

    1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  4. [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆

    转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...

  5. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  6. Java [leetcode 23]Merge k Sorted Lists

    题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...

  7. [leetcode]23. Merge k Sorted Lists归并k个有序链表

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

  8. [LeetCode]23. Merge k Sorted Lists合并K个排序链表

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

  9. leetcode 23. Merge k Sorted Lists(堆||分治法)

    Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...

随机推荐

  1. linux下svn操作(专)

    原文地址:https://www.cnblogs.com/clicli/p/5913330.html svn命令在linux下的使用SVN软件版本管理 1.将文件checkout到本地目录svn ch ...

  2. 过山车 HDU 2063 (二分图匹配裸题)

    Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生 ...

  3. LeetCode 48. Rotate Image (C++)

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  4. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 02

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2410] 版本控制地址   https://git.coding.net ...

  5. 冲刺ing-2

    第二次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 分配任务 蔺皓雯 编写博客,查阅资料 蔡晨旸 查阅资料 曾茜 暂无 鲁婧楠 暂无 杨池宇 暂无 成员遇到的问题 队员 问题 吴伟华 暂无 ...

  6. HDU 5191 Building Blocks

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5191 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  7. (打补丁 )patch

    前言: diff:逐行比较文件的不同,并且显示出来. patch: 打补丁工具,将补丁打到老文件里面,也就是diff左边的那个文件,使得老文件和新文件一样 格式:diff [选项] 老文件 新文件 格 ...

  8. IP地址转换32为长整型

    Programming Question: Convert an IPv4 address in the format of null-terminated C string into a 32-bi ...

  9. BZOJ5091 摘苹果(概率期望)

    大胆猜想每一步都相当于是第一步.稍微验证一下发现是对的.就做完了. #include<iostream> #include<cstdio> #include<cmath& ...

  10. Mybatis笔记三:iBatis与MyBatis区别

    转载:http://www.tuicool.com/articles/auaAru iBatis 框架的主要优势: 1.iBatis 封装了绝大多数的 JDBC 样板代码,使得开发者只需关注 SQL ...