题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
'''运行时间:29ms,占用内存:5660k'''
if listNode is None:
return []
nums = []
temp = listNode
while temp:
nums.append(temp.val)
temp = temp.next
nums.reverse()
return nums