【LeetCode】147. Insertion Sort List 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/insertion-sort-list/description/

题目描述:

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:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
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.
It repeats until no input elements remain.

Example 1:

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

Example 2:

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

题目大意

使用插入排序,对链表进行排序。

解题方法

这个算链表里边的难题了,其实就是模拟题目中图的过程,稍微有点复杂的就是指针的操作。参考了:链表插入排序里的做法,链表操作多用几个指针就方便很多了。

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def insertionSortList(self, head):
  8. """
  9. :type head: ListNode
  10. :rtype: ListNode
  11. """
  12. if not head or not head.next: return head
  13. root = TreeNode(0)
  14. root.next = head
  15. while head.next:
  16. if head.val <= head.next.val:
  17. head = head.next
  18. else:
  19. temp = head.next
  20. q = root
  21. head.next = head.next.next
  22. while q.next and q.next.val < temp.val:
  23. q = q.next
  24. temp.next = q.next
  25. q.next = temp
  26. return root.next

日期

2018 年 6 月 23 日 ———— 美好的周末要从刷题开始

【LeetCode】147. Insertion Sort List 解题报告(Python)的更多相关文章

  1. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  2. LeetCode: Insertion Sort List 解题报告

    Insertion Sort List Sort a linked list using insertion sort. SOLUTION: 使用一个dummynode 创建一个新的链,将旧的节点插入 ...

  3. [LeetCode] 147. Insertion Sort List 链表插入排序

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

  4. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

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

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

  6. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  7. 【LeetCode】148. Sort List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  9. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

随机推荐

  1. 10.Power of Two-Leetcode

    Given an integer, write a function to determine if it is a power of two. class Solution { public: bo ...

  2. java面试题目偏基础

    一.JAVA基础篇-概念1.简述你所知道的Linux:Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(An ...

  3. 非标准的xml解析器的C++实现:一、思考基本数据结构的设计

    前言: 我在C++项目中使用xml作为本地简易数据管理,到目前为止有5年时间了,从最初的全文搜索标签首尾,直到目前项目中实际运用的类库细致到已经基本符合w3c标准,我一共写过3次解析器,我自己并没有多 ...

  4. dubbo 协议的 K8s pod 存活探针配置

    背景 某项目采用微服务架构,dubbo 框架,K8s 方式部署. 其中 HTTP 协议由网关应用统一处理,大部分应用仅提供 dubbo 协议. 目标 应用某个实例(pod)状态异常时,尝试自动重启恢复 ...

  5. 日常Java 2021/11/21

    Java文档注释 Java支持三种注释方式.前两种分别是Ⅱ和/产*,第三种被称作说明注释,它以产开始,以*I结束.说明注释允许你在程序中嵌入关于程序的信息.你可以使用javadoc工具软件来生成信息, ...

  6. 学习java 7.20

    学习内容: Stream流 Stream流的生成方式 中间操作方法 终结操作方法 Stream流的收集操作 类加载 类加载器的作用 将.class文件加载到内存中,并为之生成对应的java.lang. ...

  7. Template Metaprogramming in C++

    说实话,学习C++以来,第一次听说"Metaprogramming"这个名词. Predict the output of following C++ program. 1 #in ...

  8. 【Linux】【Basis】文件系统

    FHS:Filesystem Hierarchy Standard Web site: https://wiki.linuxfoundation.org/lsb/fhs http://www.path ...

  9. Restful、SOAP、RPC、SOA、微服务之间的区别

    什么是Restful Restful是一种架构设计风格,提供了设计原则和约束条件,而不是架构,而满足这些约束条件和原则的应用程序或设计就是 Restful架构或服务. 主要的设计原则: 资源与URI ...

  10. spring mvc访问html页面404报错解决

    <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springfram ...