剑指offer-从尾到头打印链表-链表-python

题目描述

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
把链表依次放入list里面,反向打印
# -*- 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 []
        l = []
        while listNode:
            l.append(listNode.val)
            listNode = listNode.next
        return l[::-1]
        

 

posted @ 2019-12-06 09:22  ayew  阅读(127)  评论(0编辑  收藏  举报