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

题解:

  归并思想。

Solution 1

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int n = lists.size();
if (n < )
return nullptr; for (int i = ; i < n ; i *= ) {
int len = i;
for (int j = ; j + len < n; j += *len) {
lists[j] = mergeTwoLists(lists[j], lists[j + len]);
}
} return lists[];
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode dummy(-);
ListNode *cur = &dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2; return dummy.next;
}
};

Solution 2

  利用最小堆,c++的优先队列priority_queue。

 class Solution {
public:
struct cmp {
bool operator () (ListNode *a, ListNode *b) {
return a->val > b->val;
}
};
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode*, vector<ListNode*>, cmp> q;
for (auto list : lists) {
if (list)
q.push(list);
} ListNode *head = NULL, *pre = NULL, *tmp = NULL;
while (!q.empty()) {
tmp = q.top();
q.pop();
if (!pre)
head = tmp;
else
pre->next = tmp;
pre = pre->next;
// 此节点所在数组仍有元素,则添加进最小堆中
if (tmp->next)
q.push(tmp->next);
}
return head;
}
};

转自:Grandyang

【LeetCode】023. Merge k Sorted Lists的更多相关文章

  1. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  2. 【LeetCode】23. Merge k Sorted Lists

    合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...

  3. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  4. 【一天一道LeetCode】#23. Merge k Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. 【原创】leetCodeOj --- Merge k Sorted Lists 解题报告

    题目地址: https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题目内容: /** * Definition for singly-linke ...

  6. 【Lintcode】104.Merge k Sorted Lists

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  7. 【LeetCode】21. Merge Two Sorted Lists

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. 【leetcode】 21. Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  9. 【LeetCode】021. 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 ...

随机推荐

  1. xcode编译时,有第三方库时,编译设置build active architecture only问题

    本文转载至 http://blog.csdn.net/ysysbaobei/article/details/16371263 编译静态库 1.http://blog.csdn.net/dengdeng ...

  2. spring BeanFactory加载xml配置文件示例

    项目目录结构如下: HelloWorld.java package com.thief.demo; public class HelloWorld { public void sayHello() { ...

  3. Android设备各种使用尺寸整理

    // 获取屏幕的宽度.高度 Display defDip = getWindowManager().getDefaultDisplay(); int disWidth = defDip.getWidt ...

  4. mysql如何监测是否命中索引?

    使用执行计划. 什么是执行计划? EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARN ...

  5. 20179209《Linux内核原理与分析》第十二周作

    缓冲区溢出漏洞实验 缓冲区溢出简介 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器 ...

  6. vue 计算属性和监听器

    一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...

  7. PAT 1065. 单身狗(25)

    “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数:随后N行 ...

  8. Power Designer体验之旅

    版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/wang13667539325/article/details/36025245 从某种程度上说.不论 ...

  9. Springmvc注解启用

      http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...

  10. Python基础(4)_字典、集合、bool值

    三.字典 定义:{key1:value1,key2:value2},key-value结构,key必须是不可变类型,或者可hash 基本形式:key:value特性: 1.可存放多个值 2.可修改指定 ...