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. 20155316 2016-2017-2 《Java程序设计》第7周学习总结

    教材学习内容总结 1. 时间与日期 1.1 时间的度量 GMT -> UT -> TAI -> UTC 英文 缩写 Greenwich Mean Time GMT Universal ...

  2. kafka入门(3)- SpringBoot集成Kafka

    1.引入依赖 <dependency> <groupId>org.springframework.kafka</groupId> <artifactId> ...

  3. php imagecreatetruecolor()方法报未定义错误解决方法

    更多内容推荐微信公众号,欢迎关注: php练习生成验证码方法时,使用php的 imagecreatetruecolor() 方法 报错 Fatal error: Uncaught Error: Cal ...

  4. 2016.6.17——Valid Parentheses

    Valid Parentheses 本题收获: 1.stack的使用 2.string和char的区别 题目: Given a string containing just the character ...

  5. linux 配置免密码登陆

    在使用scp命令传输的时候需要密码 配置免密码登陆 ssh-keygen -t rsa (四个回车) 执行命令完成后,会生成两个文件id_rsa(私钥).id-rsa.pub(公钥) 将公钥拷贝到要免 ...

  6. SQL 存储过程分页

    CREATE PROC p_Team_GetTemaList @pageindex INT , @pagesize INT , @keywords VARCHAR(200) , --模糊查询 名称 标 ...

  7. 解决修改表结构,添加外键时出现“约束冲突”的错误

    由于表结构更改,使用新建表,现有部分表需要更改外键,将引用更改到新建表的相应字段.在更改过程中,部分表出现如下错误提示: ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束 ' ...

  8. poj3636

    题意:每个物品有两个属性:长和宽(长宽不可互换).如果一个物品的长和宽均大于另一个物品,则这个物品可以罩住另一个物品,用这种罩住物品的方法将物品分组,一组之内的物品可以一个罩住一个的全部罩起来.问最少 ...

  9. WCF - Autofac IOC

    /// <summary> /// IOC实例提供者,基于AutoFac /// /// </summary> public class IocInstanceProvider ...

  10. python网络编程-optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...