Sort List

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

                  Have you been asked this question in an interview?                   Yes               说明:归并排序: 时间 O(nlogn),空间 O(1). 每次将链表一分为二, 然后再合并。快排(用两个指针)
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9.  
  10. ListNode* getMid(ListNode *head) {
  11. ListNode *first, *second, *preFirst;
  12. preFirst = first = second = head;
  13. while(second != NULL) {
  14. preFirst = first;
  15. first = first->next;
  16. if(second->next == NULL) break;
  17. second = second->next->next;
  18. }
  19. preFirst->next = NULL;
  20. return first;
  21. }
  22.  
  23. void Merge(ListNode *head1, ListNode *head2) {
  24. if(head1->val > head2->val) {
  25. int tem = head1->val;
  26. head1->val = head2->val;
  27. head2->val = tem;
  28. }
  29. ListNode *p = head1;
  30. while(p->next != NULL && head2 != NULL) {
  31. if(p->next->val >= head2->val) {
  32. ListNode *q = p->next;
  33. p->next = head2;
  34. head2 = head2->next;
  35. p->next->next = q;
  36. p = p->next;
  37. } else {
  38. p = p->next;
  39. }
  40. }
  41. if(head2 != NULL)
  42. p->next = head2;
  43. }
  44.  
  45. void MergeSort(ListNode *head) {
  46. if(head == NULL || head->next == NULL) return;
  47. ListNode *mid = getMid(head);
  48. MergeSort(head);
  49. MergeSort(mid);
  50. Merge(head, mid);
  51. }
  52.  
  53. class Solution {
  54. public:
  55. ListNode *sortList(ListNode *head) {
  56. MergeSort(head);
  57. return head;
  58. }
  59. };

Insertion Sort List

Sort a linked list using insertion sort.

说明: 与顺序表不同的时,每次找插入位置时从头开始走。

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *insertionSortList(ListNode *head) {
  12. ListNode *p, *q;
  13. for(q = head; q != NULL; q = q->next) {
  14. for(p = head; p != q; p = p->next) {
  15. if(p->val >= q->val) {
  16. int tem = p->val;
  17. p->val = q->val;
  18. q->val = tem;
  19. }
  20. }
  21. }
  22. return head;
  23. }
  24. };
 
 

9. Sort List && Insertion Sort List (链表排序总结)的更多相关文章

  1. insertion sort list (使用插入排序给链表排序)

    Sort a linked list using insertion sort. 对于数组的插入排序,可以参看排序算法入门之插入排序(java实现),遍历每个元素,然后相当于把每个元素插入到前面已经排 ...

  2. 148. Sort List (java 给单链表排序)

    题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn) ...

  3. [leetcode sort]147. Insertion Sort List

    Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...

  4. [LeetCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...

  5. [LeetCode] 148. Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...

  6. 跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort)

    跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后 ...

  7. [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  8. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. 【链表】Insertion Sort List

    题目: Sort a linked list using insertion sort. 思路: 插入排序是一种O(n^2)复杂度的算法,基本想法相信大家都比较了解,就是每次循环找到一个元素在当前排好 ...

随机推荐

  1. CodeForces 688B-Lovely Palindromes

    题意: 给出一串数字,要你输出它的回文数,就这么简单. 分析: 可以用数组去做,也可以用reversed()函数(这个更简单). 代码如下: #include <iostream> #in ...

  2. 001 今天开始系统学习C#

    2016-01-16 之前只是大概了解过c#语言,感觉掌握不牢靠.现在开始系统学习C#.现以该博客作为学习笔记,方便后续查看.C# 目标:系统掌握c#知识 时间:30天 范围:C#基础,Winform ...

  3. Day14 summary

    Since I am writing blog in Ubuntu which has not installed Chinese language package, this blog will b ...

  4. 永不消逝的电波(二)HackRF入门:家用无线门铃信号重放

    0×00 前言 在第一篇文章:永不消逝的电波(一):无线电入门篇 我们了解了一下无线电的发展史以及无线电的一些物理知识,在第二篇里我们将用HackRF录制家用门铃的无线信号,然后重放门铃信号. 门铃从 ...

  5. Libgdx 开发指南(1.1) 应用框架——生命周期

    生命周期 Libgdx应用有一个定义好的生命周期,控制着整个应用的状态,例如creation, pausing, resuming, disposing ApplicationListener 开发者 ...

  6. MySQL字符集乱码

    学数据库,最让人丧气的就是字符集的问题了,一旦出问题,就会有砸电脑的冲动,特别是在修改很多次字符集后依然不成功的时候! 我用的数据库软件是MySQL 5.1.28.最初出问题的时候,是这样的: 情景一 ...

  7. 【Python】实现5!+4!+3!+2!+1!

    #!/usr/bin/env python #-*- coding:utf-8 -*- def factorial_add(n): empty_list=[] #定义一个空列表 for i in ma ...

  8. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  9. Makefile 开发环境全能管家

    变量的应用: CC=gcc RM=rm EXE=main.exe OBJS=目标 伪目标的应用: .PHONY:clean 自动变量的应用: $@:表示一个规则的目标 $^:表示的是规则中的所有的先决 ...

  10. An internal error occured during :"C/C++" . java.lang.NullPointerException

    用eclipse 导入cocos2d项目的时候报了这个错,导致项目在eclipse 里面是空的,反复导入也不行. 解决办法,把其他正常项目里面的proj.android目录下面的.cproject文件 ...