说明:链表逆序,是将链表中的单向链表逆序,双向链表逆序和正序都是一样的,所以没有任何意义。
代码:
class Node(object):
def __init__(self):
self.value = None
self.next = None
def __str__(self):
return str(self.value)
def revise_loop(head):
if not head or not head.next:
return head
pre = None
while head:
next = head.next
head.next = pre
pre = head
head = next
return pre
测试代码:
if __name__=='__main__':
three = Node()
three.value = 3
two = Node()
two.value = 2
two.next = three
one = Node()
one.value = 1
one.next = two
head = Node()
head.value = 0
head.next = one
new_head = revise_loop(head)
while new_head:
print (new_head.value,)
new_head = new_head.next
结果: