class Node(object):
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def reverse(head):
if head == None or head.next == None:
return head
pre = None
next = None
while head != None:
next = head.next
head.next = pre
pre = head
head = next
return pre
if __name__ == '__main__':
l1 = Node(1)
l1.next = Node(2)
l1.next.next = Node(3)
l2 = reverse(l1)
print(l2.value, l2.next.value, l2.next.next.value)
网友评论