题目

147. Insertion Sort List

Sort a linked list using insertion sort.

A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

  1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

解题思路

平时插入排序用的是数组,现在用链表。思路是一样的,时间复杂度还是n^2.

  1. 先创建一个空的链表,为结果链表;
  2. 遍历当前链表;
  3. 创建结果链表的当前位置,和下一个位置;
  4. 找到需要插入当前位置的元素,在已有结果链表的位置;
  5. 暂存下一个节点;
  6. 以后结果链表的当前位置指向当前位置,当前位置指向结果链表的下一个位置;
# Definition for singly-linked list.
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next
class InsertionSortList:def insertionSortList(self, head: ListNode) -> ListNode:result = ListNode()current = headwhile current:# At each iteration, we insert an element into the resulting list.pre = resultnext = result.next# find the position to insert the current nodewhile next:if current.val < next.val:breakpre = nextnext = next.nextitemNext = current.next# insert the current node to the new listpre.next = currentcurrent.next = next# moving on to the next iterationcurrent = itemNextreturn result.next

算法:链表实现插入排序Insertion Sort List相关推荐

  1. [转载] python实现基本算法之插入排序(Insertion Sort)

    参考链接: Python中的插入排序insertion sort 基本算法之插入排序(Insertion Sort) 基本算法-02.插入排序(Insertion Sort)算法 冒泡排序已经发布,大 ...

  2. python实现排序算法_python实现·十大排序算法之插入排序(Insertion Sort)

    简介 插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 算法实现步骤 从第一个元素开 ...

  3. C语言插入排序Insertion Sort算法(附完整源码)

    插入排序Insertion Sort算法 插入排序Insertion Sort算法的完整源码(定义,实现,main函数测试) 插入排序Insertion Sort算法的完整源码(定义,实现,main函 ...

  4. C语言以递归实现插入排序Insertion Sort算法(附完整源码)

    以递归实现插入排序Insertion Sort算法 以递归实现插入排序Insertion Sort算法的完整源码(定义,实现,main函数测试) 以递归实现插入排序Insertion Sort算法的完 ...

  5. python sort 逆序_python实现·十大排序算法之插入排序(Insertion Sort)

    简介 插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 算法实现步骤 从第一个元素开 ...

  6. 插入排序(Insertion Sort)-Java实现

    插入排序(Insertion Sort)算法简介: 插入排序是一种丛序列左端开始依次对数据进行排序的算法.在排序过程中,左侧的数据陆续归位,而右侧留下的就是还未被排序的数据. 插入排序(Inserti ...

  7. 插入排序(Insertion Sort)

    维基百科:http://zh.wikipedia.org/wiki/插入排序 算法思想: 若数组A[n]的前n-1个数已经有序,我们只需把第n个元素插入到适当的位置即可.易分析得算法的时间复杂度为Ο( ...

  8. 插入排序Insertion sort 2

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

  9. java算法在工作,我在北京找工作(三):java实现算法2 直接插入排序+不可变类...

    2013年9月3日 貌似看的排序算法实现的有点没难度,但还是一步一步稳扎稳打的来. 1.直接插入排序 直接插入排序(Insertion Sort)的基本思想:将数组分为有序区和无序区,每次将一个无序区 ...

  10. 数据结构与算法之三直接插入排序

    直接插入排序的基本思想是指定输入数据的个数,从键盘输入给数组a[],从数组中第2个数a[1]与第1个数a[0]开始比较起,如果第2个数a[1]比第1个数a[0]小,则把第2个数a[1]赋给一个临时变量 ...

最新文章

  1. poj1743(后缀数组+二分--不可重叠最长重复子串)
  2. 蹭热度?罗永浩:下一个创业项目是“元宇宙公司”
  3. js获取session_学习后端鉴权系列: 基于Cookie, Session认证
  4. 阿里月薪50k招AI工程师,看到要求我傻眼了!
  5. 使用公用计算机的用户如何,如何通过设置权限来管理公用电脑?
  6. 【大数据部落】R语言RFM模型在电商行业的应用
  7. 一名技术leader的工作随笔
  8. oracle财务系统表,Oracle ERP 财务模块表结构.ppt
  9. 商业的10个最佳Android应用程序模板
  10. 云杰恒指:7.19恒指期货早盘资讯
  11. 训练人物和摩托车的yolov4-tiny模型教程
  12. JAVA课程设计个人博客 学生成绩管理 201521123014 黄绍桦
  13. 通过读取csv/xml数据并且结合使用allure展示测试报告,验证开发中的add()和reduct()操作(在@allure.story分别实现相加减)
  14. javascript如何监听 form.submit()事件
  15. Linux Signal (2): signal函数
  16. 基于Carsim和Simulink的SIL软件在环仿真验证
  17. CCS软件的基本使用(以MSP430为)
  18. 【代码审计】MIPCMS 远程写入配置文件Getshell
  19. 【温故而知新:文件操作】C#的文件读写相关
  20. java打印Excel表格

热门文章

  1. ipad编程软件c语言2020,‎App Store 上的“计算机等级考试C语言版 2020最新”
  2. exifinterface.setattribute设置不上去_电脑自动开机怎么设置
  3. mockito简单教程
  4. 使用Python解析JSON详解
  5. x=n; y=1; while(x=(y−1)∗(y−1)) y++; 以上程序的时间复杂度为 ?
  6. Windows 10部署与管理指南(1)之环境准备篇
  7. Lean Startup实战
  8. 计算机专业英语信息系统,信息系统项目管理师英语复习资料:计算机专业英语汇总[5]...
  9. 开启cdn后无法显示字体图标——CDN服务器跨域问题
  10. wordpress中文路径出现404错误的解决办法