输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Stack: def __init__(self): self.stack =[] def stackIN(self,x): self.stack.append(x) def stackOut(self): return self.stack.pop() def isEmpty(self): return len(self.stack)==0 class Solution: # 返回从尾部到头部的列表值序列,例如[1,2,3] def printListFromTailToHead(self, listNode): stack = Stack() while listNode !=None: stack.stackIN(listNode.val) listNode = listNode.next printlist = [] while not stack.isEmpty(): printlist.append(stack.stackOut()) return printlist