Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)
题目: 合并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 == null|| lists.length == 0)
return null;
if(lists.length == 1)
return lists[0];
int end = lists.length - 1;
int begin = 0;
while(end > 0) // 将lists的头和尾进行归并,然后将结果存放在头,头向后移动,尾向前移动,直到begin=end,则已经归并了一半,此时将begin=0,继续归并
{
begin = 0;
while(begin < end)
{
lists[begin] = combineTwoList(lists[begin], lists[end]);
begin ++;
end --;
}
} return lists[0];
}
public ListNode combineTwoList(ListNode head1, ListNode head2) // head1和head2为头节点的两个单链表的合并
{
if(head1 == null && head2 == null) // 如果两个单链表都不存在,则返回null
return null;
if(head1 == null) // 如果head1不存在,则直接返回head2
return head2;
if(head2 == null)
return head1;
ListNode pHead = null;
if(head1.val > head2.val) // 根据head1与head2的值,决定头节点
{
pHead = head2;
pHead.next = combineTwoList(head1, head2.next);
}
else
{
pHead = head1;
pHead.next = combineTwoList(head1.next, head2);
}
return pHead;
}
}
Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)的更多相关文章
- Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)
题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1. 如果两个链表都空,则返回null; 2. 如果链表1空,则返回链表2的 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- [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 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- [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 ...
- 023 Merge k Sorted Lists 合并K个有序链表
合并K个有序链表,并且作为一个有序链表的形式返回.分析并描述它的复杂度. 详见:https://leetcode.com/problems/merge-k-sorted-lists/descripti ...
- [Leetcode] Merge two sorted lists 合并两已排序的链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- swift基础-3
fallthrough 贯穿 case 可以不必写break 如果不需要知道区间内 每一项的值 可以使用 下划线 —— 来代替变量名 忽略 对该值的访问 for index in 1...5{ p ...
- 用jQuery实现jsonp跨域
跨域的安全限制都是指浏览器端来说的.服务器端是不存在跨域安全限制的,所以通过本机服务器端通过类似httpclient方式完成“跨域访问”的工作,然后在浏览器端用AJAX获取本机服务器端“跨域访问”对应 ...
- <Android HAL 之路> HAL 简介
HAL层概述 名称: HAL, Hardware Abstracting Layer,中文名字:硬件抽象层. 作用:对Linux内核驱动程序的封装,向上提供接口,屏蔽低层的实现细节.向上衔接Andro ...
- specrate 与specspeed 的区别
What is the difference between a "rate" and a "speed" metric?There are several d ...
- Android Studio maven-metadata.xml 卡着不动原因和解决方法
头一天好好的,第二天就卡着了. 一直在这个地方不动,如果停止就会报 Error:Could not run build action using Gradle distribution ‘https: ...
- codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...
- intellij idea自定义快捷键
intellij idea自定义快捷键比较简单,自带. 首先进入idea,File-->Settings-->Live Templates. 点击右上方的“+”,第一次需要创建一个分组. ...
- java基础—线程(二)
一.线程的优先级别
- 简单的邮件发送mail.jar
public class MailSender { final static Logger logger = Logger.getLogger(MailSender.class); /** * 发送简 ...
- 01_10_SERVLET如何连接Mysql数据库
01_10_SERVLET如何连接Mysql数据库 1. 实现类 public void doGet(HttpServletRequest request, HttpServletResponse r ...