题目描述:

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

解题思路:

分治方法。将K个List不断地分解为前半部分和后半部分。分别进行两个List的合并。最后将合并的结果合并起来。

代码如下:

/**
* 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.length == 0)
return null;
return divide(lists, 0, lists.length - 1);
} public ListNode divide(ListNode[] l1, int left, int right) {
if(left < right){
int mid = (left + right) / 2;
return mergeTwoLists(divide(l1, left, mid), divide(l1, mid + 1, right));
}
return l1[left];
} public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode list = new ListNode(0);
ListNode tmp = list;
while (l1 != null || l2 != null) {
if (l1 == null) {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
} else if (l2 == null) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
if (l1.val < l2.val) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
}
}
tmp = tmp.next;
}
return list.next;
}
}

Java [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. [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 ...

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

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

  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. poj 3415 Common Substrings 后缀数组+单调栈

    题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...

  2. 2、[转]WPF与WinForm的比较

    http://www.cnblogs.com/KnightsWarrior/archive/2010/07/09/1774059.htmlhttp://www.cnblogs.com/zenghong ...

  3. 详解Google-ProtoBuf中结构化数据的编码

    本文的主要内容是google protobuf中序列化数据时用到的编码规则,但是,介绍具体的编码规则之前,我觉得有必要先简单介绍一下google protobuf.因此,本文首先会介绍一些google ...

  4. easy ui window 相关属性

    <div class="easyui-window" title="提示" style="width:550px;height:500px;pa ...

  5. Entity Framework 安装出现问题

    Entity Framework 详情请看: http://ulfqbpl.blog.163.com/blog/static/8778355220126272473276/

  6. 839. Optimal Marks - SPOJ

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  7. 【数学/扩展欧几里得/Lucas定理】BZOJ 1951 :[Sdoi 2010]古代猪文

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  8. The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemG:Give Me the Number

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971 题意:将输入的英文数字表达转化为阿拉伯数字. #include< ...

  9. uva 10051

    将每一个分解为六个两面的 简单地dp 回溯输出路径..... #include <cstdio> #include <cstring> #include <cmath&g ...

  10. Unity3D的几种坐标系

    原地址:http://www.cnblogs.com/martianzone/p/3371789.html http://www.cnblogs.com/88999660/archive/2013/0 ...