[leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/
题意:对链表进行插入排序。
解题思路:首先来对插入排序有一个直观的认识,来自维基百科。
代码循环部分图示:
代码:
class Solution:
# @param head, a ListNode
# @return a ListNode
def insertionSortList(self, head):
if not head:
return head
dummy = ListNode(0) #为链表加一个头节点
dummy.next = head
curr = head
while curr.next:
if curr.next.val < curr.val: #如果链表是升序的,那么curr指针一直往后移动
pre = dummy #直到一个节点的值小于前面节点的值
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 dummy.next
[leetcode]Insertion Sort List @ Python的更多相关文章
- 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 】 python 实现
题目: Sort a linked list using insertion sort. 代码:oj测试通过 Runtime: 860 ms # Definition for singly-linke ...
- LeetCode解题报告:Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. leetcode subject思路:标准的插入排序.考察一下链表的操作. 对 ...
随机推荐
- Oracle登录
一.安装Oracle 二.打开Database Configuration Assistant 三.打开SQL Plus 1.打开SQL Plus 2.输入用户名sys 3.输入口令1 as sysd ...
- Oracle数据库的基本查询
本文用的是Oracle 10g数据库,利用PL/SQL Developer的集成开发环境(安装可以自行百度) Oracle数据库 ---> 数据库实例 ---> 表空间(逻辑单位)( ...
- Spring boot整合shiro权限管理
Apache Shiro功能框架: Shiro聚焦与应用程序安全领域的四大基石:认证.授权.会话管理和保密. #,认证,也叫作登录,用于验证用户是不是他自己所说的那个人: #,授权,也就是访问控制,比 ...
- JAVAEE——宜立方商城08:Zookeeper+SolrCloud集群搭建、搜索功能切换到集群版、Activemq消息队列搭建与使用
1. 学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步索引库. a) Activemq b) 发送消息 c) 接收消息 2. 什么是So ...
- redis集群错误解决:/usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is already busy (Redis::CommandError)
错误信息: /usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb:79:in `call': ERR Slot 15495 is al ...
- python中的super( test, self).__init__()
python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...
- python开发_tkinter_单选按钮
这篇blog主要是描述python中tkinter的单选按钮操作 下面是我做的demo 运行效果: ====================================== 代码部分: ===== ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 镜面对称 博弈论
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- rapidjson的使用
1.包含头文件 #include "rapidjson/stringbuffer.h"#include "rapidjson/document.h"#inclu ...
- Tasker 模拟重复点击按钮
http://stackoverflow.com.80bola.com/questions/21544271/android-tasker-app-advance-input-shell-comman ...