Title:

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

排好序的,然后merge,很容易让人联想到归并排序。可以将k个序列按照二分进行分割,然后当长度为1时返回一个排好序的序列,最后将两个序列merge

class Solution{
public:
ListNode* merge(ListNode* list1, ListNode* list2){
ListNode head(),*tail = &head;
while (list1 != NULL && list2 != NULL){
if (list1->val < list2->val){
tail->next = list1;
tail = list1;
list1 = list1->next;
}else{
tail->next = list2;
tail = list2;
list2 = list2->next;
}
}
if (list1 != NULL){
tail->next = list1;
}
if (list2 != NULL){
tail->next = list2;
}
return head.next;
}
ListNode* divide(int l,int r,vector<ListNode* > &lists){
int m = (l + r) / ;
if (l < r){
return merge(divide(l,m,lists),divide(m+,r,lists));
}else
return lists[l];
}
ListNode* mergeKLists(vector<ListNode* > &lists){
int k = lists.size();
if ( == k)
return NULL;
return divide(,k-,lists);
}
};

LeetCode: MergekSortedLists的更多相关文章

  1. leetcode — merge-k-sorted-lists

    import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; /** * Source : ht ...

  2. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  3. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

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

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

  7. [LeetCode]题解(python):023-Merge k Sorted Lists

    题目来源: https://leetcode.com/problems/merge-k-sorted-lists/ 题意分析: 给定k个有序的链表,将这些链表整合成一个新的有序链表. 题目思路: 前面 ...

  8. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  9. LeetCode 题目总结/分类

    LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...

随机推荐

  1. 剑指offer--面试题10

    题目:求整数二进制表示中1的个数. 分析:此题直接考查二进制表示与位运算!!! 正数和负数的二进制表示不同!在计算机中,正数的二进制表示即为通常所写的二进制:而负数的二进制表示则用补码表示,即原码的反 ...

  2. java中判空

    一.概述 java中判等似乎很简单,==用来判断对象引用(内存地址)是否相同,equals用来判断值是否相同.你可以试用String对象轻松区分这一点. 那么在null判等(也就是判空操作)时呢? 可 ...

  3. E-R图向关系模式的转换

    转自: http://hi.baidu.com/qicaiqinxian/blog/item/a8bb0bdf31ae081b63279887.html E-R图向关系模型转换时犯糊涂了,找到下面这篇 ...

  4. 【Asp.Net MVC--资料汇总】杂七杂八

    Html.RenderPartial与Html.RenderAction的区别 http://blog.sina.com.cn/s/blog_8278b1800100zkn0.html ASP.NET ...

  5. 【.Net--资料】

    1.http://msdn.microsoft.com/zh-cn/dn338450 2..NET Technology Guidance http://www.microsoft.com/net/n ...

  6. prefix springmvc

    设置了@RequestMapping("/jsp/info.do"),也可以写成"jsp/info.act"不影响 retuen "index&quo ...

  7. hdu 4324 Triangle LOVE(拓扑排序,基础)

    题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...

  8. 黑马程序员-C#学习笔记

    ---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- C#学习笔记 1..NET/.do ...

  9. Gulp实战和原理解析

    Gulp实战和原理解析(以weui作为项目实例)http://i5ting.github.io/stuq-gulp/

  10. Netty 的Downstream 和 Upstream

    Netty的Downstream 和 Upstream 如果一个event从第一个handler传递直到最后一个handler就是 Upstream 相反的如果一个event从最后一个handler传 ...