[Linked List]Sort List
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);
}
};
[Linked List]Sort List的更多相关文章
- [LeetCode] Sort List 排序 sort
Sort a linked list in O(n log n) time using constant space complexity. Hide Tags Linked List Sort ...
- 模板化的七种排序算法,适用于T* vector<T>以及list<T>
最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...
- LeetCode 刷题顺序表
Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...
- LeetCode Question Difficulty Distribution
参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...
- leetcode难度及面试频率
转载自:LeetCode Question Difficulty Distribution 1 Two Sum 2 5 array sort set ...
- LeetCode难度与出现频率
转载自:LeetCode Question Difficulty Distribution 1 Two Sum 2 5 array sort set Two ...
- leetcode难度及频率
1 Two Sum 2 5 array sort set Two Pointers 2 Add Two Numbers 3 4 linked l ...
- LeetCode难度和面试频率(转)
转自:http://www.cnblogs.com/ywl925/p/3507945.html ID Question Diff Freq Data Structure Algorit ...
- LeetCode 题解汇总
前言 现如今,对于技术人员(软开.算法等)求职过程中笔试都是必不可少的(免笔试的除外,大部分人都需要笔试),而笔试一般组成都是选择.填空.简答题.编程题(这部分很重要),所以刷题是必不可少的:对于应届 ...
随机推荐
- bootstrap使用中遇到的问题(二)
1.ie8不支持carousel组件, 解决方法:将jquery换为jquery1版本,具体原因不清楚~~~~~ 2.ie8不支持background-color:rgba(); 解决方法:这样写代码 ...
- 疯狂Android第一章:Android环境配置以及基本概念
第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...
- 系统开发中按下Enter键登录系统
转载来自:http://www.jb51.net/article/54308.htm 系统开发中按下Enter键登录系统,即就是监听键盘,当按下Enter键后调用登录按钮的click()事件. JS方 ...
- null值的判断
select * from Students where Address IS null --判断address是nulselect * from Students where Address is ...
- SQL使用记录
Q:怎么删掉sql server登录时的用户名?(仅仅是删掉那个登录时的提示) A:先关闭数据库登录引擎,然后删除:%AppData%\Microsoft\Microsoft SQL Server\1 ...
- node.js入门(三)调式
1.安装调式工具 打开命令行工具,输入以下内容,然后回车. npm install -g node-inspector 等待安装成功呢后,我们就可以使用 node-debug 文件名 这个命令来调式我 ...
- JAVA异常使用_每个人都曾用过、但未必都用得好
一.抛出异常 vs. 返回错误代码 有人说“Well, an exception is a goto.”,但也有人言“makes the code simpler by visibly separat ...
- JS delete 用法(删除对象属性及变量)
1,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...
- JavaScript 字符串常用操作纪要
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
- jQuery 验证实例(shopnc二次开发)
shopnc 商家用户实现添加用户与前台用户分离, jQuery 验证实例 equalTo:等于 <div id="saleRefund" show_id="1&q ...