otal Accepted: 59473 Total Submissions: 253637 Difficulty: Medium

Sort a linked list in O(n log n) time using constant space complexity.

 
递归版本,代码比较好理解,并不符合题目要求,题目要求常量的空间
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeSortList(ListNode* head1,ListNode* head2)
{
if(head1==NULL){
return head2;
}
if(head2==NULL){
return head1;
}
ListNode* newHead = head1->val < head2->val ? head1:head2;
newHead->next = head1->val < head2->val ? mergeSortList(head1->next,head2):mergeSortList(head1,head2->next);
return newHead;
}
int getListLength(ListNode *head)
{
int cnt = ;
while(head){
cnt++;
head = head->next;
}
return cnt;
}
ListNode* sortList(ListNode* head) {
int len = getListLength(head);
if(len<=){
return head;
}
int cnt = len/;
ListNode* p = head,*pre=NULL;
while(cnt--){
pre = p;
p = p->next;
}
ListNode* head2 = p;
pre->next = NULL;
ListNode* head1 = sortList(head);
head2 = sortList(head2);
return mergeSortList(head1,head2);
}
};
Next challenges: (M) Insertion Sort List

[Linked List]Sort List的更多相关文章

  1. [LeetCode] Sort List 排序 sort

    Sort a linked list in O(n log n) time using constant space complexity. Hide Tags Linked List Sort   ...

  2. 模板化的七种排序算法,适用于T* vector<T>以及list<T>

    最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...

  3. LeetCode 刷题顺序表

    Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...

  4. LeetCode Question Difficulty Distribution

    参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...

  5. leetcode难度及面试频率

    转载自:LeetCode Question Difficulty Distribution                 1 Two Sum 2 5 array sort           set ...

  6. LeetCode难度与出现频率

    转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two ...

  7. leetcode难度及频率

                    1 Two Sum 2 5 array sort           set Two Pointers   2 Add Two Numbers 3 4 linked l ...

  8. LeetCode难度和面试频率(转)

    转自:http://www.cnblogs.com/ywl925/p/3507945.html    ID Question   Diff  Freq  Data Structure  Algorit ...

  9. LeetCode 题解汇总

    前言 现如今,对于技术人员(软开.算法等)求职过程中笔试都是必不可少的(免笔试的除外,大部分人都需要笔试),而笔试一般组成都是选择.填空.简答题.编程题(这部分很重要),所以刷题是必不可少的:对于应届 ...

随机推荐

  1. bootstrap使用中遇到的问题(二)

    1.ie8不支持carousel组件, 解决方法:将jquery换为jquery1版本,具体原因不清楚~~~~~ 2.ie8不支持background-color:rgba(); 解决方法:这样写代码 ...

  2. 疯狂Android第一章:Android环境配置以及基本概念

    第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...

  3. 系统开发中按下Enter键登录系统

    转载来自:http://www.jb51.net/article/54308.htm 系统开发中按下Enter键登录系统,即就是监听键盘,当按下Enter键后调用登录按钮的click()事件. JS方 ...

  4. null值的判断

    select * from Students where Address IS null --判断address是nulselect * from Students where Address is ...

  5. SQL使用记录

    Q:怎么删掉sql server登录时的用户名?(仅仅是删掉那个登录时的提示) A:先关闭数据库登录引擎,然后删除:%AppData%\Microsoft\Microsoft SQL Server\1 ...

  6. node.js入门(三)调式

    1.安装调式工具 打开命令行工具,输入以下内容,然后回车. npm install -g node-inspector 等待安装成功呢后,我们就可以使用 node-debug 文件名 这个命令来调式我 ...

  7. JAVA异常使用_每个人都曾用过、但未必都用得好

    一.抛出异常 vs. 返回错误代码 有人说“Well, an exception is a goto.”,但也有人言“makes the code simpler by visibly separat ...

  8. JS delete 用法(删除对象属性及变量)

    1,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...

  9. JavaScript 字符串常用操作纪要

    JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...

  10. jQuery 验证实例(shopnc二次开发)

    shopnc 商家用户实现添加用户与前台用户分离, jQuery 验证实例 equalTo:等于 <div id="saleRefund" show_id="1&q ...