• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python数据结构之翻转链表

Python数据结构之翻转链表

作者:lqh 字体:[增加 减小] 来源:互联网

lqh 通过本文主要向大家介绍了python 链表,python实现链表,python 单链表,python中链表,python 循环链表等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

翻转一个链表

样例:给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null

一种比较简单的方法是用“摘除法”。就是先新建一个空节点,然后遍历整个链表,依次令遍历到的节点指向新建链表的头节点。

那样例来说,步骤是这样的:

1. 新建空节点:None
2. 1->None
3. 2->1->None
4. 3->2->1->None

代码就非常简单了:

""" 
Definition of ListNode 
 
class ListNode(object): 
 
 def __init__(self, val, next=None): 
  self.val = val 
  self.next = next 
""" 
class Solution: 
 """ 
 @param head: The first node of the linked list. 
 @return: You should return the head of the reversed linked list. 
     Reverse it in-place. 
 """ 
 def reverse(self, head): 
  temp = None 
  while head: 
   cur = head.next 
   head.next = temp 
   temp = head 
   head = cur 
  return temp 
  # write your code here 
</div>

当然,还有一种稍微难度大一点的解法。我们可以对链表中节点依次摘链和链接的方法写出原地翻转的代码:

""" 
Definition of ListNode 
 
class ListNode(object): 
 
 def __init__(self, val, next=None): 
  self.val = val 
  self.next = next 
""" 
class Solution: 
 """ 
 @param head: The first node of the linked list. 
 @return: You should return the head of the reversed linked list. 
     Reverse it in-place. 
 """ 
 def reverse(self, head): 
  if head is None: 
   return head 
  dummy = ListNode(-1) 
  dummy.next = head 
  pre, cur = head, head.next 
  while cur: 
   temp = cur 
   # 把摘链的地方连起来 
   pre.next = cur.next 
   cur = pre.next 
   temp.next = dummy.next 
   dummy.next = temp 
  return dummy.next 
  # write your code here 
</div>

需要注意的是,做摘链的时候,不要忘了把摘除的地方再连起来

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

</div>

您可能想查找下面的文章:

  • Python 实现链表实例代码
  • Python 数据结构之旋转链表
  • Python数据结构之翻转链表
  • Python单链表简单实现代码
  • Python单链表简单实现代码
  • Python实现的数据结构与算法之链表详解
  • Python单链表的简单实现方法

相关文章

  • Python简单遍历字典及删除元素的方法
  • 使用C#配合ArcGIS Engine进行地理信息系统开发
  • 关于tf.reverse_sequence()简述
  • 在Django中创建动态视图的教程
  • Python3读取zip文件信息的方法
  • 简单介绍Python的轻便web框架Bottle
  • python搭建简易服务器分析与实现
  • Python随机生成信用卡卡号的实现方法
  • Pycharm学习教程(2) 代码风格
  • Python中的类与对象之描述符详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python检测远程端口是否打开的方法
    • Python读写Excel文件的实例
    • Python 登录网站详解及实例
    • python实现带验证码网站的自动登陆实现代码
    • 双向RNN:bidirectional_dynamic_rnn()函数的使用详解
    • Python实现图像几何变换
    • python实现读取命令行参数的方法
    • Python3中常用的处理时间和实现定时任务的方法的介绍
    • Python中的ConfigParser模块使用详解
    • 举例讲解Python中metaclass元类的创建与使用

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有