代码如下:
class Node(object):
def __init__(self,data,next=None):
self.data = data
self.next = next
def __init__(self,data,next=None):
self.data = data
self.next = next
def fun(node):
p = node
m = p
cur = node.next
while cur:
tmp = cur.next
cur.next = p
p = cur
cur = tmp
m.next = None
return p
if __name__ == "__main__":
l1 = Node(1)
l1.next = Node(2)
l1.next.next = Node(3)
l1.next.next.next = Node(4)
l1 = fun(l1)
while True:
print(l1.data)
l1 = l1.next
if l1 == None:
break
l1 = Node(1)
l1.next = Node(2)
l1.next.next = Node(3)
l1.next.next.next = Node(4)
l1 = fun(l1)
while True:
print(l1.data)
l1 = l1.next
if l1 == None:
break