题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
-- coding:utf-8 --
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution: def init(self): self.a = [] def h(self, listNode): if listNode == None: return self.h(listNode.next) self.a.append(listNode.val)
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
# write code here
self.arr = []
cur = listNode
while cur:
self.arr.append(cur.val)
cur = cur.next
return self.arr[::-1]