leetcode 【 Insertion Sort List 】 python 实现
题目:
Sort a linked list using insertion sort.
代码:oj测试通过 Runtime: 860 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a ListNode
def insertionSortList(self, head): if head is None or head.next is None:
return head dummyhead = ListNode(0)
dummyhead.next = head curr = dummyhead.next
while curr.next is not None:
if curr.next.val < curr.val:
pre = dummyhead
while pre.next.val < curr.next.val:
pre = pre.next
tmp = curr.next
curr.next = tmp.next
tmp.next = pre.next
pre.next = tmp
else:
curr = curr.next return dummyhead.next
思路:
首先要知道插入排序的原理。
对于单链表来说,需要做指针的交换。
小白记忆插入排序的原理只有一句话:类比扑克牌抓牌,把牌按大小插入。
需要注意的地方是
不要加多余的判断,否则会超时;第一次运行的时候,我在里面的while循环条件判断上加了一句 pre != curr,结果就报超时了。
check一下逻辑,发现完全是无用判断浪费时间,去掉后就通过了。
leetcode 【 Insertion Sort List 】 python 实现的更多相关文章
- [leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
- LeetCode :: Insertion Sort List [具体分析]
Sort a linked list using insertion sort. 仍然是一个很简洁的题目,让我们用插入排序给链表排序:这里说到插入排序.能够来回想一下, 最主要的入门排序算法.就是插入 ...
- LeetCode: Insertion Sort List 解题报告
Insertion Sort List Sort a linked list using insertion sort. SOLUTION: 使用一个dummynode 创建一个新的链,将旧的节点插入 ...
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode解题报告:Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. leetcode subject思路:标准的插入排序.考察一下链表的操作. 对 ...
随机推荐
- python数据类型、输入输出、运算符、条件判断、循环
变量以及类型 变量:存储程序运行中的数据,变量有3个要素:变量名.变量类型.变量值.python属于弱类型语言,不需要声明变量类型. [root@localhost python]# ipython3 ...
- 阻止Bootstrap 模态框(Modal)点击空白处时关闭
默认情况下点击空白处时会关闭模态框,添加data-backdrop="static"后可以阻止关闭
- STL中的map和hash_map
以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- leetcode——2
1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...
- UVA 11990 ``Dynamic'' Inversion (序列分治)
26天以前做过的一道题,之前的做法是分治预处理,树套树在线修改,复杂度为O(nlogn+m*logn*logn),代码量较大. 本来想学习一下cdq分治的,看到论文上的凸包.斜率就暂时放一边了,只知道 ...
- js中随机数获取
// 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(randomNum1); // 函数结果为入参的整数部 ...
- 第四篇、Swift_Podfile文件配置格式
# Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...
- OI,我的决心
虽然从初一就开始NOIP,但沉溺于游戏编程等各种乱七八糟的技术,一直没对算法有过透彻的研究. ——————————简单的来说就是水过了—————————— 我生于一个弱省,就读于一所弱校(我们全区的都 ...
- shell脚本中case的用法
shell脚本中case选择语句可以结合read指令实现比较好的交互应答操作,case接收到read指令传入的一个或多个参数,然后case根据参数做选择操作. case的语法如下 case $char ...