Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

依次拼接

复杂度

时间 O(N) 空间 O(1)

思路

该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后。最后如果有没比较完的,说明另一个list的值全比这个list剩下的小,而且拼完了,所以可以把剩下的直接全部接上去。

代码

 1 public class Solution {
 2     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 3         // 创建一个dummy头,从后面开始接
 4         ListNode dummy = new ListNode(0);
 5         ListNode curr = dummy;
 6         // 依次比较拼接
 7         while(l1 != null && l2 != null){
 8             if(l1.val <= l2.val){
 9                 curr.next = l1;
10                 l1 = l1.next;
11             } else {
12                 curr.next = l2;
13                 l2 = l2.next;
14             }
15             curr = curr.next;
16         }
17         // 把剩余的全拼上去
18         if(l1 == null){
19             curr.next = l2;
20         } else if (l2 == null){
21             curr.next = l1;
22         }
23         return dummy.next;
24     }
25 }

Merge k Sorted Lists

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

优先队列

复杂度

时间 O(NlogK) 空间 O(K)

思路

当我们归并k个列表时,最简单的方法就是,对于每次插入,我们遍历这K个列表的最前面的元素,找出K个中最小的再加入到结果中。不过如果我们用一个优先队列(堆),将这K个元素加入再找堆顶元素,每次插入只要logK的复杂度。当拿出堆顶元素后,我们再将它所在链表的下一个元素拿出来,放到堆中。这样直到所有链表都被拿完,归并也就完成了。

注意

因为堆中是链表节点,我们在初始化堆时还要新建一个Comparator的类。

代码

 1 public class Solution {
 2     public ListNode mergeKLists(ListNode[] lists) {
 3         if(lists.length == 0) return null;
 4         ListNode dummy = new ListNode(0);
 5         PriorityQueue<ListNode> q = new PriorityQueue<ListNode>(11, new Comparator<ListNode>(){
 6             public int compare(ListNode n1, ListNode n2){
 7                 return n1.val - n2.val;
 8             }
 9         });
10         // 初始化大小为k的堆
11         for(int i = 0; i < lists.length; i++){
12             if(lists[i] != null) q.offer(lists[i]);
13         }
14         ListNode curr = dummy;
15         while(!q.isEmpty()){
16             // 拿出堆顶元素
17             curr.next = q.poll();
18             curr = curr.next;
19             // 将堆顶元素的下一个加入堆中
20             if(curr.next != null){
21                 q.offer(curr.next);
22             }
23         }
24         return dummy.next;
25     }
26 }
时间: 2024-10-12 21:21:07

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists的相关文章

[LeetCode]题解(python):023-Merge k Sorted Lists

题目来源: https://leetcode.com/problems/merge-k-sorted-lists/ 题意分析: 给定k个有序的链表,将这些链表整合成一个新的有序链表. 题目思路: 前面我们已经给出了两个有序链表整合的做法.这里,我们不妨用归并排序的想法,把n个链表看成 n/2 和n - n/2的整合,直到n/2 <= 1.时间复杂度是 O(n * (2^log k)) = O(n * k). 代码(python): 1 # Definition for singly-linked

[Leetcode][Python]23: Merge k Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 23: Merge k Sorted Listshttps://oj.leetcode.com/problems/merge-k-sorted-lists/ Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ===Comments by

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K Sorted Lists

LeetCode 21: Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

LeetCode 23. 合并K个排序链表(Merge Two Sorted Lists)

23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k Sorted Lists困难 示例: 输入: [ ??1->4->5, ??1->3->4, ??2->6] 输出: 1->1->2->3->4->4->5->6 Java 实现 public class ListNode { int va

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个. 此题有如下几种解法: 1.最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中.基于此,有人通过最小堆来简化最小元素的比较. struct Compa

【一天一道LeetCode】#23. Merge k Sorted Lists

一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. (二)解题 合并K个已拍好序的链表.剑指上有合并两个已排好序的链表的算法,那么K个数,我们可以采用归并排序的思想,不过合并函数可能需要修改一下,换成合并两个已排好序的链表的方法.代码如下: /** * Definition for singly-linked

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

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =