合并 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入:
[
  1->4->5,
  1->3->4,
  2->6
]
输出: 1->1->2->3->4->4->5->6
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode MergeKLists(ListNode[] lists) {
ListNode res = new ListNode();
ListNode p = res;
List<ListNode> index = new List<ListNode>();
for(int i=; i<lists.Length; i++)
{
ListNode node = new ListNode();
node.next = lists[i];
index.Add(node);
}
while(index.Count != )
{
List<int> mins = new List<int>();
int min = int.MaxValue;
for(int i=index.Count-; i>=; i--)
{
if(index[i].next != null)
{
if (index[i].next.val < min)
{
min = index[i].next.val;
mins.Clear();
mins.Add(i);
}
else if (index[i].next.val == min)
{
mins.Add(i);
}
}
}
for(int i=; i<mins.Count; i++)
{
ListNode node = new ListNode(index[mins[i]].next.val);
p.next = node;
p = node;
index[mins[i]] = index[mins[i]].next;
}
for(int i = index.Count - ; i >= ; i--)
{
if(index[i].next == null)
{
index.Remove(index[i]);
}
}
}
return res.next;
}
}

0008 合并K个排序链表的更多相关文章

  1. [LeetCode] 23. 合并K个排序链表

    题目链接: https://leetcode-cn.com/problems/merge-k-sorted-lists/ 题目描述: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂 ...

  2. [Swift]LeetCode23. 合并K个排序链表 | Merge k Sorted Lists

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

  3. 合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: 1-&g ...

  4. 合并K个排序链表(java实现)

    题目: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: ...

  5. LeetCode(23):合并K个排序链表

    Hard! 题目描述: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2-> ...

  6. LeetCode题解-23 合并K个排序链表 Hard

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1->1-&g ...

  7. Leetcode题库——23.合并k个排序链表

    @author: ZZQ @software: PyCharm @file: mergeKLists.py @time: 2018/10/12 19:55 说明:合并 k 个排序链表,返回合并后的排序 ...

  8. leetcode 23. 合并K个排序链表 JAVA

    题目: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: ...

  9. 代码题(14)— 合并有序链表、数组、合并K个排序链表

    1.21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出 ...

随机推荐

  1. [LeetCode&Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  2. [LeetCode&Python] Problem 415. Add Strings

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  3. Egret_时间与运行

    1.加载资源 2.将显示对象添加到显示列表 /////////////*****************************动态帧频******************************** ...

  4. 20155219付颖卓《网络对抗》EXP7网络欺诈技术防范

    实验后回答问题 1.通常在什么场景下容易受到DNS spoof攻击 在公共共享网络里,并且同一网段可以ping通的网络非常容易被攻击. 2.在日常生活工作中如何防范以上两攻击方法 不在不信任的公开网络 ...

  5. windows下用XShell远程控制ubuntu时连接失败

    主机和Ubuntu可以相互Ping通,但是XShell远程控制失败. 查看Ip地址方法:ifconfig 解决方案: 1.查看,关闭防火墙状态:sudo ufw disable 2.开放22端口:su ...

  6. E-R视图中有关图形的用法

    这里先推荐几款相对好用的画E-R图的软件: 第一款为:  这是一款在线的画流程图软件 第二款为:亿图图示 第三款为:Visio ER图是在设计数据库之前,明白数据之间的相互关系,理清数据之间的逻辑而需 ...

  7. Css新增内容

    css3新增属性 可节省设计时间的属性 border-color:控制边框的颜色,并且有更大的灵活性,可以产生渐变效果 border-image:控制边框图像 border-radius:能产生类似圆 ...

  8. Codeforces 1105B:Zuhair and Strings(字符串水题)

    time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...

  9. Jdbc来操作事物 完成模拟银行的转账业务

    创建JDBC工具类 package cn.aa4_2.JDBCUtils; import java.io.FileReader; import java.io.IOException; import ...

  10. AutoCompleteExtender 使用示例

    绑定 KeyValuePair,网上很多例子,没有找到绑定键值对的,msdn上有例子,备忘一下. using FirstElite.Verify.Entity; using System; using ...