python 单向链表逆序_python 实现单向链表的倒序

本文适合有一定数据结构基础的人阅读(就是你要懂链表是什么)

在C/C++中,通常采用“指针+结构体”来实现链表;而在Python中,则可以采用“引用+类”来实现链表。

链表L中的每个元素都是一个对象,每个对象有一个关键字key和一个指针:next

题目:单链表倒序,并输出新的链表。

解题思路:

在C的时候我们使用三个指针遍历单链表,逐个链接点进行反转。python也可以采用类似的思路,其实语言并不是很大的关系。

代码(包含建立链表、添加节点、打印链表)

class Node:#定义节点

def __init__(self, initdata):

self.__data = initdata

self.__next = None

def getData(self):

return self.__data

def getNext(self):

return self.__next

def setData(self, newdata):

self.__data = newdata

def setNext(self, newnext):

self.__next = newnext

class SinCycLinkedlist:

def __init__(self):

self.head = Node(None)

self.head.setNext(self.head)

def add(self, item):

temp = Node(item)

temp.set

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值