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. canvas、image src、data url、blob file conversion

    //canvas.toDataURL('image/jpeg'), and convert to blob,blob is a File Object. but UC don't support fu ...

  2. logfile提示stale错误解决方法

    产生该错误的原因解释如下: Explanation: ============ A stale redo log file is one that Oracle believes might be i ...

  3. Struts2注解学习1

    这是开博的第一篇,我希望每天把我学到的东西记录下来,成为一个知识库,方便以后的学习和分享 在项目中看到用struts2注解来做,很方便,做了一个用户登录的例子 1.加载所需jar包 commons-f ...

  4. Android Adapter代码片

    /** * Adapter for grid of coupons. */ private static class CouponAdapter extends BaseAdapter { priva ...

  5. Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃

    在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java. ...

  6. Oracle之PLSQL总结

    基本数据类型变量 1. 基本数据类型     Number 数字型     Int 整数型     Pls_integer 整数型,产生溢出时出现错误 Binary_integer 整数型,表示带符号 ...

  7. 初学swift笔记 继承(十)

    import Foundation /* 继承 class childClass: FatherClass{ } 优点 代码重用 缺点 增加程序的藕合度 父类的改变会影响子类 只能单继承 */ cla ...

  8. knockout简单实用教程3

    在之前的文章里面介绍了一些KO的基本用法.包括基本的绑定方式,基本的ko的绑定语法包括text绑定,html绑定等等(如有不明请参照上两篇文章),下面呢介绍一下关于ko的其他方面的知识.包括比较特殊绑 ...

  9. android TextView EditTextView一些技巧使用 (视图代码布局)

    android TextView 是最常用的控件 可以用作普通的显示,还可以用作有显示文字的按钮,用作有显示图片的图文组合 1. 图文组合 xml 中: <TextView android:id ...

  10. Linux install Maven3

    1. Download JDK1.6 http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u31-download-1501634 ...