标题:单链表、reversed函数
只看楼主
bubble_soup
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2018-1-21
结帖率:0
 问题点数:0 回复次数:0 
单链表、reversed函数
def __reversed__(self):
                if self.head is None or self.head.next is None:
                        return None
                curl = self.head
                prev = None
                while curl:
                        temp = curl.next
                        curl.next = prev
                        prev = curl
                        curl = temp
                return prev


lyst = LinkedList()
head = lyst.Node()
node1 = lyst.Node(1)
node2 = lyst.Node(2)
node3 = lyst.Node(3)
lyst.head = head
head.next = node1
node1.next = node2
node2.next = node3

lyst1 = LinkedList(reversed(lyst))
lyst1.printNode()
print("=========================")
lyst.printNode()

----------------------------------------------------------------------------------------------------------


2
1
None
=========================

[Finished in 0.1s]
为什么在reversed后lyst就没有了(printNode时没有结点。)???

另,顺便问下。如果要在LinkedList类中实现reverse()(不是__reversed__()),应该返回什么?(返回self吗)
搜索更多相关主题的帖子: reversed 函数 head next Node 
2018-04-11 12:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-485959-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.528853 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved