题目来源:

  https://leetcode.com/problems/insertion-sort-list/


题意分析:

  用插入排序排序一个链表。


题目思路:

  这题没什么好说的,直接用插入排序就行。


代码(python):

 # Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return head
tmp = ListNode(0)
tmp.next,p = head,head
while p.next:
if p.next.val < p.val:
tmp1 = tmp
while tmp1.next.val < p.next.val:
tmp1 = tmp1.next
t = p.next
p.next = t.next
t.next = tmp1.next
tmp1.next = t
else:
p = p.next
return tmp.next

[LeetCode]题解(python):147-Insertion Sort List的更多相关文章

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

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

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

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

  3. LeetCode OJ 147. Insertion Sort List

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

  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 ----- java

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

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

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

  7. 【leetcode】147. Insertion Sort List

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

  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. [leetcode sort]147. Insertion Sort List

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

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

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

随机推荐

  1. KZ--NSString、NSMutableString

            //NSString初始化的几种方法(3种方法)         //1.         NSString *str2 = [[NSString alloc] init];      ...

  2. hdu2368Alfredo's Pizza Restaurant

    Problem Description Traditionally after the Local Contest, judges and contestants go to their favour ...

  3. java Timer 使用小结

    Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务.这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少. 任务的调用通过起的子线程进 ...

  4. a 标签的四个伪类

    link        有链接属性时visited    链接地址已被访问过active     被用户激活(在鼠标点击与释放之间发生的事件)hover      其鼠标悬停 <!DOCTYPE ...

  5. Prisma

    AI修图艺术:Prisma背后的奇妙算法 | 深度 投递人 itwriter 发布于 2016-07-30 12:47 评论(2) 有712人阅读 原文链接 [收藏] « » 雷锋网按:本文作者系图普 ...

  6. 【Chromium中文文档】OS X 沙箱设计

    OS X 沙箱设计 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/OSX ...

  7. Linux05--Shell程序设计01

    1.Shell脚本介绍 基本介绍: shell脚本是一个可执行的纯文本文件,由多个shell命令组成. 命令的执行是从上而下,从左而右的分析和执行 命令,参数间的多个空白也会被忽略 #是注释 #!用于 ...

  8. DIV+CSS规范命名

    一.命名规则说明: 1).所有的命名最好都小写2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",id="div ...

  9. Codeforces 306B

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  10. PLA能收敛的证明

    题:如果资料D线性可分,PLA如何保证最后能得到最优解. 思路:假设$w_f$能够分割资料D,$w_{t+1}$经过更新$w_{t+1}=w_t + y_{n(t)}x_{n(t)}$后,与$w_f$ ...