从尾到头打印链表
从尾到头打印链表
http://www.nowcoder.com/questionTerminal/d0267f7f55b3412ba93bd35cfa8e8035
简单列表保存
def printListFromTailToHead(self, listNode):
# write code here
res=[]
while listNode:
res.append(listNode.val)
listNode=listNode.next
return res[::-1] #逆序打印 递归
def printListFromTailToHead(self, listNode):
res=[]
def printListnode(listNode):
# write code here
if listNode:
printListnode(listNode.next)#先递归到最后一层
res.append(listNode.val)#添加值,退出函数,返回到上一层函数中的这行,继续添加值
printListnode(listNode)
return res
查看12道真题和解析