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

思路:

用归并排序。设输入链表为S,则先将其拆分为前半部分链表A,后半部分链表B。注意,要把A链表的末尾置为NULL。即要把链表划分为两个独立的部分,防止后面错乱。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *sortList(ListNode *head) {
if(head == NULL) return NULL;
ListNode * p = head;
int n = ;
while(p->next != NULL){n++; p = p->next;}
return MergeSort(head, n);
} ListNode * MergeSort(ListNode * head, int n)
{
if(n == )
return NULL;
else if(n == )
{
return head;
}
else
{
int m = n / ;
//下面这种划分的方法很挫 应用后面大神的方法
ListNode * headb = head;
ListNode * p = NULL;
int k = ;
while(k < m + )
{
p = headb;
headb = headb->next;
k++;
}
p->next = NULL; ListNode * A = MergeSort(head, m);
ListNode * B = MergeSort(headb, n - m);
return Merge(A, B);
}
} ListNode * Merge(ListNode * A, ListNode * B)
{
ListNode * C, * head;
if(A->val < B->val)
{
C = A; A = A->next;
}
else
{
C = B; B = B->next;
}
head = C; while(A != NULL && B != NULL)
{
if(A->val < B->val)
{
C->next = A; A = A->next;
}
else
{
C->next = B; B = B->next;
}
C = C->next;
} if(A != NULL)
{
C->next = A;
}
else
{
C->next = B;
}
return head;
} void createList(ListNode * &head)
{
int n;
cin >> n;
if(n != )
{
head = new ListNode(n);
createList(head->next);
}
}
}; int main()
{
Solution s;
ListNode * L = NULL;
s.createList(L); ListNode * LS = s.sortList(L); return ;
}

大神精简版代码,关键注意划分过程

ListNode *sortList(ListNode *head) {
if (head == NULL || head->next == NULL)
return head; // find the middle place
ListNode *p1 = head;
ListNode *p2 = head->next;
while(p2 && p2->next) {
p1 = p1->next;
p2 = p2->next->next;
}
p2 = p1->next;
p1->next = NULL; return mergeList(sortList(head), sortList(p2));
} ListNode *mergeList(ListNode* pHead1, ListNode* pHead2) {
if (NULL == pHead1)
return pHead2;
else if (NULL == pHead2)
return pHead1; ListNode* pMergedHead = NULL; if(pHead1->val < pHead2->val)
{
pMergedHead = pHead1;
pMergedHead->next = mergeList(pHead1->next, pHead2);
}
else
{
pMergedHead = pHead2;
pMergedHead->next = mergeList(pHead1, pHead2->next);
} return pMergedHead;
}

【leetcode】Sort List (middle)的更多相关文章

  1. 【leetcode】Sort Colors(middle)☆

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

  2. 【LeetCode】Sort Colors 解题报告

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

  3. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  4. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  5. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  6. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  7. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  8. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

  9. 【leetcode】Combination Sum (middle)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

随机推荐

  1. 什么是SEM?

    SEM是Search Engine Marketing的英文缩写,其中文意思就是搜索引擎营销.台湾和香港.澳门也称为搜寻销售,意思都差不多.SEM更多强调的是综合手段在搜索引擎上的企业传播和促进和销售 ...

  2. Express开发实例(2) —— Jade模板引擎

    前一篇通过helloworld,简单介绍了Express中的开发,本篇继续深入的学习express的模板. 关于Jade的用法,网上有很多,本篇参考:Jade语法 安装相关模块 在实验代码前,应该先安 ...

  3. Windows疑难杂症之开机无法显示桌面。

    开机无法显示桌面可能有以下两种情况. 1.系统故障或病毒引起explorer.exe无法加载启动. 2.注册表故障造成默认的值不是explorer.exe.(可能是安装了某些软件造成此问题) 3,某开 ...

  4. jquery动态改变my97日期格式

    $('#qsrq').unbind('focus'); $('#zzrq').unbind('focus'); $('#qsrq').bind('focus', function () { Wdate ...

  5. Swift-打开其它Storyboard中的自定义模态窗口

    本文的方法针对OS X应用开发. 如果想在某个ViewController中,用模态窗口的方式,打开某个Storyboard中定义的WindowController.可用以下方式. let story ...

  6. cpu利用率和cpu 队列

    SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果已经无法反映在高并发和高压力下SIP的运行状况,因此需要重新作压力测试.跟在测试人员后面做了快一周的压力测试,压力测试的报告也正式出炉,本 ...

  7. NFS工作原理及配置文件详解

    nfs工作原理流程       如上图所示,当访问程序通过NFS客户端向NFS服务端存取文件时,其请求数据流程如下几点:     1.首先用户访问网站程序,由程序在NFS客户端上发出NFS文件存取功能 ...

  8. linux 下开放端口问题

    Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables   [root@l ...

  9. Try-Catch机制使用场景分析

    (一)在什么场景下加Try-Catch机制   1)以业务逻辑功能为单位,在最上层加Try-Catch机制.为什么要这样做呢?这主要是增加程序的健壮性,防止因抛出异常过多,导致程序崩溃. try { ...

  10. phpcms如何判断用户是否登录

    首先要获取userid  <?php         $userid= param::get_cookie('_userid'); ?> 然后再判断是否为空 {if $userid}    ...