[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 题解汇总
前言 现如今,对于技术人员(软开.算法等)求职过程中笔试都是必不可少的(免笔试的除外,大部分人都需要笔试),而笔试一般组成都是选择.填空.简答题.编程题(这部分很重要),所以刷题是必不可少的:对于应届 ...
随机推荐
- checkbox,radio,selected相关操作
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- java字符串数组进行大小排序
若是将两个字符串直接比较大小,会包:The operator > is undefined for the argument type(s) java.lang.String, java.lan ...
- (重要) html概念之 input:name与id详解
实例: 带有两个文本字段和一个提交按钮的 HTML 表单: <form action="form_action.asp" method="get"> ...
- Win7如何添加局域网内的网络打印机
win+R或开始找到运行,在运行框中输入打印机所在的局域网内的IP地址. 这时会打开一个界面.如图 右键要选择的打印机.连接.这时会显示正在安装打印机驱动.如图 开始菜单->设备和打印机 找到刚 ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...
- Spring配置扫描mybatis的mapper文件注意:
一般会将不业务的mapper文件放到不同的包中: spring配置扫描就需要配置下面的方式(两个*): <!-- mybatis文件配置,扫描所有mapper文件 --> <bean ...
- mutex 和 spinlock 对比
理论上: mutex和spinlock都是用于多进程/线程间访问公共资源时保持同步用的,只 是在lock失败的时候处理方式有所不同.首先,当一个thread 给一个mutex上锁失败的时候,threa ...
- linux命令find应用
基本语法: find path -option [-print ] [-exec -ok command ] {} \; find命令的参数: pathname: find ...
- 混合使用Azure LB和ILB访问相同web服务(3)
接下来我们来配置Azure Load balancer,就是面向公网的负载均衡器: 1.在该测试中,为了保持内网访问和外网访问一样的体验,本地端口和public端口和ILB一样,同样是80: PS C ...
- 关于Asp.net超时,延长读取sql server数据库的超时时间!(已解决)
昨天,接到客户反映说应用报“超时时间已到.在操作完成之前超时时间已过或服务器未响应”问题.从网上了一些资料,发现这个问题还是很普遍的.主要有以下两种解决方法: 第一种方法:在web.config中加上 ...