Merge k Sorted Lists Leetcode Java
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
使用分治法,时间复杂度是nlogk, n是所有元素个数的总和,k是k个lists,
这种方法和依次merge每一个list的方法的时间复杂度不同。
如果第一个list有n-k个元素,其余每个list是1个元素,
两两分治合并,每个元素参与了logk次合并,
如果是依次合并(第一个和第二个合并,之后的结果和第三个合并,以此类推),n-k个元素需要参与k次合作,
这样复杂度就会增加。
/**
* 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 mergeHelper(lists, 0, lists.length - 1);
} public ListNode mergeHelper(ListNode[] list, int start, int end){
if (start == end){
return list[start];
} int mid = start + (end - start) / 2;
ListNode left = mergeHelper(list, start, mid);
ListNode right = mergeHelper(list, mid + 1, end);
return mergeTwoSortedList(left, right);
} public ListNode mergeTwoSortedList(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while(list1 != null && list2 != null){
if(list1.val < list2.val) {
tail.next = list1;
tail = list1;
list1 = list1.next;
}else {
tail.next = list2;
tail = list2;
list2 = list2.next;
}
} if(list1 != null) {
tail.next = list1;
} if(list2 != null) {
tail.next = list2;
}
return dummy.next;
}
}
画了一张图
Merge k Sorted Lists Leetcode Java的更多相关文章
- 23. Merge k Sorted Lists - LeetCode
Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...
- Merge k Sorted Lists [LeetCode]
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. S ...
- Merge Two Sorted Lists leetcode java
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 蜗牛慢慢爬 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 ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- LeetCode 023 Merge k Sorted Lists
题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【LeetCode练习题】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
随机推荐
- js获取当前域名及获取页面完整地址并做判断
<script language="javascript"> //获取域名 hostName = window.location.host; host2=documen ...
- uC/OS-II标志(flag)块
/*************************************************************************************************** ...
- Spring解析
Spring还是蛮有技术含量的,可以自己用代码实践一遍,找了一篇实践的案例: http://qingwengang.iteye.com/blog/621678 先mark下,等后面有时间了实践一遍. ...
- JMeter 安装与使用基本教程
JMeter使用了不同技术和协议,是一款可以进行配置和执行负载测试.性能测试和压力测试的工具. 什么是JMeter? JMeter是Apache组织的开放源代码项目,它是功能和性能测试的工具,100% ...
- HTTP1.0与HTTP1.1的区别
HTTP/1.1与HTTP/1.0的区别 下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原 ...
- git push.default is unset
warning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple' ...
- 一些分享微博,qq啥的js
<div class="bdsharebuttonbox" style="float:right"><a href="#" ...
- step 3 socket
socket 网络通讯三要素 IP地址(主机名) 网络中设备的标示 不易记忆,可以用主机名 本地回环地址:127.0.0.1 主机名:localhost 每台计算机都有一个 127.0.0.1 如果 ...
- 如何在editplus中配置ctags?
首先要说明的是, 在editPlus中的ctags功能确实是没有 vs vim等中的好用. 最主要的原因 是它不能直接在文件中 跳转. 而是要通过一个另外的框来实现, 这就大大的降低了跳转的速度和使用 ...
- hdu4970 Killing Monsters (差分数列)
2014多校9 1011 http://acm.hdu.edu.cn/showproblem.php?pid=4970 Killing Monsters Time Limit: 2000/1000 M ...