Merge k Sorted Lists [LeetCode]
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Summary: Finds the smallest node every round, then links it to the one sorted list.
ListNode *mergeKLists(vector<ListNode *> &lists) {
ListNode * head = NULL;
ListNode * pre_node = NULL;
while(true){
//find the smallest one
ListNode * smallest_node = NULL;
for(auto item : lists) {
if(item != NULL){
if(smallest_node == NULL || item->val < smallest_node->val )
smallest_node = item;
}
}
if(smallest_node == NULL)
break; if(pre_node == NULL){
pre_node = smallest_node;
head = pre_node;
}else{
pre_node -> next = smallest_node;
pre_node = pre_node -> next;
} for(int i = ; i< lists.size(); i ++){
if(lists[i] == smallest_node)
lists[i] = lists[i] -> next;
}
}
return head;
}
Merge k Sorted Lists [LeetCode]的更多相关文章
- 23. Merge k Sorted Lists - LeetCode
Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...
- Merge k Sorted Lists Leetcode Java
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 使 ...
- 蜗牛慢慢爬 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练习题】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 Two Sorted Lists && Merge k Sorted Lists
1. Merge Two Sorted Lists 题目链接 题目要求: Merge two sorted linked lists and return it as a new list. The ...
- 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 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 shoul ...
随机推荐
- vimperator学习教程
有人总是说Vim难学,而Vimperator把火狐变成了VIM,所以很多人就认为只有那些VIM的狂热者才会用这个插件,但实际上并不是这样.Vimperator在安装之后,只要稍加练习,就可以让我们的浏 ...
- [C和指针]第三部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- STORM_0003_linux_zookeeper_storm_遇到的几个问题
1. 首先是花费时间在windows的eclipse下面安装fatjar因为是新版的缘故,装了很久才装上. 后来发现其实mvn可以打包出没有依赖的jar包 2. 然后是按照在ubuntu环境中的mvn ...
- 移动端图表插件jChart.js的修改
bug1: 折线图,设置datasetGesture : true时,Y轴的刻度值居然会变.会变也就算了,居然没地方设置不能变. bug2: 折线图,设置tap.point事件,和datasetGes ...
- 简单几步配置gitlab
简单几步配置gitlab 之前配置gitlab需要很多步骤,要装apache2.ruby.tomcat.mysql等一片东西.有没有更简单的方式呢?现在可以借助bitnami,简化了很多. 可以参考v ...
- ZOJ-2342 Roads 二分图最小权值覆盖
题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...
- Nginx RTMP 专题
说明: 记录器 - 记录器名称 path - 记录文件路径(recorded file path) (/tmp/rec/mystream-1389499351.flv)filename - 省略目录的 ...
- springmvc前后端传值
@pathvible 后端传值(rest风格) exp: @requestMapping("/view/{userId}") public String getiew(@Parth ...
- For Exam (Java常用设计模式) 介绍
一 创建型模式 工厂模式(Factory): 定义一个用以创建对象的接口 抽象工厂模式(Abstract Factory): 提供一个创建一系列相关或相互依赖对象的接口 单例模式(Singleton) ...
- Mysql delete,truncate,drop
1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...