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;
}

最新文章

  1. MP3文件信息批量更改器
  2. 用Python实现多核心并行计算
  3. 【BZOJ】3997: [TJOI2015]组合数学
  4. iOS通过ARC管理内存(内容根据iOS编程编写)
  5. typename
  6. mysql数据库误删除后的数据恢复操作说明
  7. Tilera 服务器上hadoop单机版测试
  8. 关于字符串 “*****AB**C*D*****” 中前缀、后缀和中间 &#39;*&#39; 的处理
  9. java应用uploadify 3.2丢失session
  10. &#39;vt100&#39;: unknown terminal type.
  11. 使用r.js进行前端repuirejs的合并压缩
  12. android中百度地图定位的实现方法(仅适用于真机+WIFI联网环境)
  13. 基于BrokerPattern服务器框架
  14. HTML DOM 改变 HTML 内容
  15. App Store10大被拒理由
  16. python的标准数据类型
  17. --- Android 设置为A2DP 接收器
  18. 【洛谷P2142 高精度减法】
  19. 自己对Java的一点看法
  20. TCP 协议简析

热门文章

  1. Inside the c++ object module 阅读摘要
  2. 构建spring+mybatis+redis架构时遇到的小异常
  3. 学习(主题或切入点)checklist1
  4. 【转】GATK使用方法详解(包含bwa使用)
  5. VPN和SSH的原理区别
  6. java之Date
  7. CSS中font-size、font-family、line-height顺序以及简写属性
  8. DOM之操作技术
  9. Codeforces 259 B - Little Pony and Sort by Shift
  10. iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用