Sort a linked list using insertion sort.

利用插入排序对一个链表进行排序

思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可以从要插入的位置开始从后往前找合适的位置

 class Solution(object):
def insertionSortList(self, head):
dummy = ListNode(-1)
dummy.next,cur= head,head
while cur and cur.next:
if cur.val > cur.next.val:
head = dummy
while head.next.val < cur.next.val:
head = head.next
head.next,cur.next.next,cur.next = cur.next,head.next,cur.next.next
else:
cur = cur.next
return dummy.next

[leetcode sort]147. Insertion Sort List的更多相关文章

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

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  3. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  4. 【刷题-LeetCode】147 Insertion Sort List

    Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...

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

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

  6. Java for LeetCode 147 Insertion Sort List

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

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

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

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

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

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

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

随机推荐

  1. Java容器Map接口

    Map接口容器存放的是key-value对,由于Map是按key索引的,因此 key 是不可重复的,但 value 允许重复. 下面简单介绍一下Map接口的实现,包括HashMap,LinkedHas ...

  2. [NOIP2015提高&洛谷P2678]跳石头 题解(二分答案)

    [NOIP2015提高&洛谷P2678]跳石头 Description 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之 ...

  3. 如何在Maven和Gradle中配置使用Groovy 2.4与Spock 1.0

    如何在Maven和Gradle中配置使用Groovy 2.4与Spock 1.0 原文 https://dzone.com/articles/spock-10-groovy-24 翻译 hxfiref ...

  4. UNIX网络编程 第1章:简介和TCP/IP

    1.1 按1.9节未尾的步骤找出你自己的网络拓扑的信息. 1.2 获取本书示例的源代码(见前言),编译并测试图1-5所示的TCP时间获取客户程序.运行这个程序若干次,每次以不同IP地址作为命令行参数. ...

  5. Linux内核跟踪之ring buffer的实现【转】

      转自:http://blog.chinaunix.net/uid-20543183-id-1930845.html ---------------------------------------- ...

  6. shell监控网站是否自动运行并自动重启【原创】

    shell监控网站是否自动运行并自动重启 #!/bin/bash -T www.baidu.com ];then echo "`date` 网站访问正常!" >> /r ...

  7. Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon')

    问题: error: Error: No resource found that matches the given name (at 'icon' with value '@mipmap/Icon' ...

  8. 22 Gobs of data 设计和使用采集数据的包

    Gobs of data 24 March 2011 Introduction To transmit a data structure across a network or to store it ...

  9. mysql取以当前时间为中心的任意时间段的时间戳

    例如:取当前时间后一年的时间戳 SELECT UNIX_TIMESTAMP(date_sub(curdate(),interval -1 YEAR)) SELECT UNIX_TIMESTAMP(da ...

  10. 日期时间设置 "2018-05-04T16:36:23.6341371+08:00" 格式

    using System;using System.Collections.Generic;using System.Globalization;using System.Text; namespac ...