题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。
python递归求解:
class ListNode:
def __init__(self,elem,next_=None):
self.elem=elem
self.next_=next_
#从尾到头打印链表
class printReverse:
def print_(self,p):
if p==None:
print('False')
else:
if p.next_!=None:
self.print_(p.next_)#递归
print(p.elem)
c=ListNode(3)
b=ListNode(2,c)
a=ListNode(1,b)
cc=printReverse()
cc.print_(a)