题目:输入一个链表,按链表值从尾到头的顺序返回一个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
num = []
if listNode is None:
return num
while listNode is not None:
num.append(listNode.val)
listNode = listNode.next
return num[::-1]
# 创建链表 a->b->c
a = ListNode(1)
b = ListNode(2)
c = ListNode(3)
a.next = b
b.next = c
# 实例化
demo = Solution()
print(demo.printListFromTailToHead(a))