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

  

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *merge(ListNode *head1, ListNode * head2){ if(head1 == NULL) return head2;
if(head2 == NULL) return head1;
ListNode *head = new ListNode() ;
ListNode *p, *p1, *p2;
p = head, p1 = head1, p2 = head2;
while(p1 != NULL && p2 != NULL){
if(p1->val < p2->val){
p->next = p1;
p = p1;
p1 = p1->next;
}else{
p->next = p2;
p = p2;
p2 = p2->next;
}
}
p->next = p1 == NULL ? p2 : p1;
p = head->next; delete head;
return p;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = lists.size();
if(n < ) return NULL;
int len = ;
while(len < n){
for(int i = ; i + len < n; i += len * ){
lists[i] = merge(lists[i], lists[i+len]);
}
len = len<<;
}
return lists[];
}
};

归并的思路来做。

LeetCode_ Merge k Sorted Lists的更多相关文章

  1. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  2. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  3. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  4. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  6. 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 ...

  7. leetcode-algorithms-23 Merge k Sorted Lists

    leetcode-algorithms-23 Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted ...

  8. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  9. 蜗牛慢慢爬 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 ...

随机推荐

  1. Oracle执行计划——all_rows和first_rows(n) 优化器模式

    0. 环境创建 SQL> create usertest identified by test 2 default tablespace users 3 temporary tablespace ...

  2. 用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件

    项目中经常需要用到此类型的工具 #pragma warning (disable:4786) #include <iostream> #include <map> #inclu ...

  3. XCode7中不能使用http的临时配置解决办法

    先看看iOS9新特性中关于ATS的官方文档: App Transport Security App Transport Security (ATS) enforces best practices i ...

  4. CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元

    题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...

  5. Robotium--scroll操作系列

    上下滚动 scrollDown public boolean scrollDown() Scrolls down the screen. Returns: true if more scrolling ...

  6. [Redux] Avoiding Object Mutations with Object.assign() and ...spread

    Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...

  7. how to get file from classpath using jboss7.x.1 --reference

    question: I want to convert smooks xml-java, so that i need to load source file from mobeeadmin.war/ ...

  8. git clone之后自动checkout文件处理

    这个问题发生是因为不同操作系统的行结束符不一致导致的,可在clone之后在仓库根目录修改.gitattributes文件 简单处理的话,注释* text=auto这行即可.也可根据不同系统,做相应设定 ...

  9. 去除jquery.min.map 404错误信息

    调试中出现了 jquery.min.map 404 (Not Found) 的js错误信息: 那么jquery.min.map到底是个什么呢? JQuery 官方解释 从 jQuery 1.9.0 版 ...

  10. Sql 语句添加字段、修改字段类型、默认值语法

    Sql 语句添加字段 ,) not null --修改类型 alter Table bbs ) Sql 语句修改默认值 alter table 表名 drop constraint 约束名字 --删除 ...