链接
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
solution
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
current = listNode
node_list = []
while current:
node_list.append(current.val)
current = current.next
node_list.reverse()
return node_list
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
if listNode is None:
return []
return self.printListFromTailToHead(listNode.next) + [listNode.val]