输入一个链表,从尾到头打印链表每个节点的值。
思路:1、设置一个空数组
2、从头到尾遍历链表,将值插入空数组的首位
# -*- 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
if listNode is None:
return []
ans = []
while listNode:
ans.insert(0,listNode.val)
listNode = listNode.next
return ans