链表排序,要求使用 O(nlgn) 时间,常量空间。

使用归并的思路

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *findMid(ListNode *head)
{
ListNode *mid;
ListNode *fast = head->next;
ListNode *slow = head;
while(fast && fast->next)
{
fast = fast->next;
fast = fast->next;
slow = slow->next;
}
mid = slow;
return mid;
}
ListNode *sortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *tmp = findMid(head);
ListNode *right = sortList(tmp->next);
tmp->next = NULL;
ListNode *left = sortList(head); ListNode *newHead = new ListNode(-);
newHead->next = merge(left,right);
return newHead->next;
}
ListNode *merge(ListNode *left, ListNode *right)
{
ListNode *dummy = new ListNode(-);
for(ListNode *p = dummy; left!=NULL || right!= NULL; p = p->next)
{
int leftVal = left == NULL? INT_MAX:left->val;
int rightVal = right == NULL?INT_MAX:right->val;
if(leftVal <= rightVal)
{
p->next = left;
left = left->next;
}
else
{
p->next = right;
right = right->next;
}
}
return dummy->next;
}
};

LeetCode OJ-- Sort List **@的更多相关文章

  1. [LeetCode OJ] Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  8. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  9. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  10. 待字闺中之快排单向链表;leetcode之Sort List

    题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...

随机推荐

  1. Vmware web client 5.5 控制台连接不上:Connection timed out

    因XP下安装vsphere client5.5后,无法连接远程vsphere.因此使用vsphere web client. 但是用vsphere web client打开控制台,报错:Conneti ...

  2. ZYB's Premutation POJ5592

    Problem Description ZYBZYBZYB has a premutation PPP,but he only remeber the reverse log of each pref ...

  3. AX Dynamic 2012 tabletype:TempDB使用

    LedgerJournalTmp ledgerJournalTmpJoin; LedgerJournalTransAccrual this.takeOwnershipOfTempTable(ledge ...

  4. 关掉apache2服务器日志文件

    磁盘空间突然满了,才发现是这个东西占空间.11个G的空间 修改:/etc/apache2/size-available/default <VirtualHost *:80>ServerNa ...

  5. Show "Appear Offline" in Lync

    在注册表中 HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Communicator 创建DWORD,name是EnableAppearOffline,v ...

  6. /etc/resolv.conf overwritten. Redhat/Centos

    Prevent /etc/resolv.conf from being blown away by RHEL/CentOS after customizing If you are using RHE ...

  7. java文件保存至服务器

    import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java ...

  8. openldap sshkey & 用户自定义属性

    http://qiita.com/T_Tsan/items/eeb0a9ae9b4cdeb80934 https://www.ossramblings.com/using-ldap-to-store- ...

  9. 【转】操作权限不够?教你开启Win7管理员帐户

    在Win7中进行一些设置,或修改某些文件时,经常会弹出当前帐户没有操作权限的提示,即使已经是管理员账户也不行.事实上,出于安全方面的考虑,默认情况下Win7系统的系统管理员Administrator账 ...

  10. tp框架 中的时间 查询范围

    $where['add_time'] = array(array('egt',$starttime),array('elt',$endtime),'AND');