我试图了解为反转链接列表提供的解决方案。特别是,我不明白为什么我们写的最后一行:
self.head=prev
并不是
current=prev
自从
current=self.head
我知道我的推理有缺陷,这就是我来这里寻求帮助的原因。先感谢您。
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
def reverse(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev