从尾到头打印链表 python

# -*- 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 not listNode:
            return []
        pHead = listNode
        stack = []
        while pHead:
            stack.append(pHead.val)
            pHead= pHead.next
        return stack[::-1]

 

上一篇:【剑指offer 55】链表中环的入口结点


下一篇:剑指offer——反转链表