单链表、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吗)