- Recursion
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
if listNode:
return self.printListFromTailToHead(listNode.next)+[listNode.val]
else:
return []
- Stack
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
stack = []
res = []
while listNode:
stack.append(listNode)
listNode = listNode.next
while stack:
res.append(stack.pop().val)
return res
版权声明:本文为forever_zzx原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。