剑指offer 面试6题

面试6题:

题目:从尾到头打印链表

输入一个链表,从尾到头打印链表每个节点的值。

 

解题代码:

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

 

posted @ 2018-06-12 11:26  Fintech带你飞  阅读(1848)  评论(0编辑  收藏  举报