【发布时间】:2018-12-13 06:05:00
【问题描述】:
我尝试了以下代码来反转链表,并得到一个无限循环作为错误。你能告诉我这种方法有什么问题吗?
def reverse(self):
temp = curr = self.head #curr refers to the next node
prev = None
while temp:
curr = temp.next #curr goes to the next node of temp
curr.next = temp #curr node points to its previous node temp
prev = temp #prev moves to the next node
temp = curr
#self.head.next = None
self.head = prev
【问题讨论】:
标签: python data-structures linked-list reverse