输入一个链表,从尾到头打印链表每个节点的值
#coding:utf-8
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
result = []
if listNode is None:
return result
while listNode.next is not None:
result.extend([listNode.val])
listNode=listNode.next
result.extend([listNode.val])
return result[::-1]
if __name__ == '__main__':
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node1.next = node2
node2.next = node3
s = Solution()
print(s.printListFromTailToHead(node1))