python算法题 链表反转详解


Posted in Python onJuly 02, 2019

链表的反转是一个很常见、很基础的数据结构题,输入一个单向链表,输出逆序反转后的链表,如图:上面的链表转换成下面的链表。实现链表反转有两种方式,一种是循环迭代,另外一种方式是递归。

python算法题 链表反转详解

第一种方式:循坏迭代

循坏迭代算法需要三个临时变量:pre、head、next,临界条件是链表为None或者链表就只有一个节点。

# encoding: utf-8
class Node(object):
def __init__(self):
self.value = None
self.next = None
def __str__(self):
return str(self.value)
def reverse_loop(head):
if not head or not head.next:
return head
pre = None 
while head:
next = head.next # 缓存当前节点的向后指针,待下次迭代用
head.next = pre # 这一步是反转的关键,相当于把当前的向前指针作为当前节点的向后指针
pre = head # 作为下次迭代时的(当前节点的)向前指针
head = next # 作为下次迭代时的(当前)节点
return pre # 返回头指针,头指针就是迭代到最后一次时的head变量(赋值给了pre)

测试一下:

if __name__ == '__main__':
three = Node()
three.value = 3
two = Node()
two.value = 2
two.next = three
one = Node()
one.value = 1
one.next = two
head = Node()
head.value = 0
head.next = one
newhead = reverse_loop(head)
while newhead:
print(newhead.value, )
newhead = newhead.next

输出:

3
2
1
0
2

python算法题 链表反转详解

第二种方式:递归

递归的思想就是:

head.next = None
head.next.next = head.next
head.next.next.next = head.next.next
...
...

head的向后指针的向后指针转换成head的向后指针,依此类推。

实现的关键步骤就是找到临界点,何时退出递归。当head.next为None时,说明已经是最后一个节点了,此时不再递归调用。

def reverse_recursion(head):
if not head or not head.next:
return head
new_head = reverse_recursion(head.next)
head.next.next = head
head.next = None
return new_head

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中使用第三方模块的教程
Apr 27 Python
Python中MySQLdb和torndb模块对MySQL的断连问题处理
Nov 09 Python
Python+Selenium自动化实现分页(pagination)处理
Mar 31 Python
高效测试用例组织算法pairwise之Python实现方法
Jul 19 Python
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
Nov 02 Python
python并发编程之线程实例解析
Dec 27 Python
Python简单实现两个任意字符串乘积的方法示例
Apr 12 Python
django配置连接数据库及原生sql语句的使用方法
Mar 03 Python
python实现猜数字游戏
Mar 25 Python
python实现图片插入文字
Nov 26 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
Mar 03 Python
Tensorflow与Keras自适应使用显存方式
Jun 22 Python
python输入多行字符串的方法总结
Jul 02 #Python
Django高级编程之自定义Field实现多语言
Jul 02 #Python
python 杀死自身进程的实现方法
Jul 01 #Python
python 判断linux进程,并杀死进程的实现方法
Jul 01 #Python
PyCharm-错误-找不到指定文件python.exe的解决方法
Jul 01 #Python
解决pycharm 工具栏Tool中找不到Run manager.py Task的问题
Jul 01 #Python
简单了解python中对象的取反运算符
Jul 01 #Python
You might like
PHP小技巧之函数重载
2014/06/02 PHP
php生成网页桌面快捷方式
2017/05/05 PHP
PHP实现的权重算法示例【可用于游戏根据权限来随机物品】
2019/02/15 PHP
json-lib出现There is a cycle in the hierarchy解决办法
2010/02/24 Javascript
推荐20家国外的脚本下载网站
2011/04/28 Javascript
图片延迟加载的实现代码(模仿懒惰)
2013/03/29 Javascript
Jquery取得iframe下内容的方法
2013/11/18 Javascript
了不起的node.js读书笔记之node的学习总结
2014/12/22 Javascript
jQuery搜索同辈元素方法
2015/02/10 Javascript
jQuery实现仿路边灯箱广告图片轮播效果
2015/04/15 Javascript
谈谈encodeURI和encodeURIComponent以及escape的区别与应用
2015/11/24 Javascript
javascript jquery对form元素的常见操作详解
2016/06/12 Javascript
JS封装的自动创建表格的实现代码
2016/06/15 Javascript
JS中type="button"和type="submit"的区别
2017/07/04 Javascript
vue项目总结之文件夹结构配置详解
2017/12/13 Javascript
[42:48]完美世界DOTA2联赛PWL S3 Magma vs INK ICE 第二场 12.11
2020/12/16 DOTA
python文件比较示例分享
2014/01/10 Python
LRUCache的实现原理及利用python实现的方法
2017/11/21 Python
Python读取YUV文件,并显示的方法
2018/12/04 Python
pandas DataFrame 交集并集补集的实现
2019/06/24 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
使用python+whoosh实现全文检索
2019/12/09 Python
tensorflow常用函数API介绍
2020/04/19 Python
python访问hdfs的操作
2020/06/06 Python
html5的新增的标签和废除的标签简要概述
2013/02/20 HTML / CSS
canvas绘制圆角头像的实现方法
2019/01/17 HTML / CSS
美国眼镜网站:LensCrafters
2020/01/19 全球购物
RIP版本1跟版本2的区别
2013/12/30 面试题
小车司机岗位职责
2013/11/25 职场文书
优秀志愿者事迹材料
2014/02/03 职场文书
党员承诺践诺书
2014/05/20 职场文书
药店营业员岗位职责
2015/04/14 职场文书
工程催款通知书
2015/04/17 职场文书
个人求职意向书
2015/05/11 职场文书
委托开发合同书(标准版)
2019/08/07 职场文书
Python list列表删除元素的4种方法
2021/11/01 Python