剑指Offer23-从尾到头打印链表

之前刷LeetCode就已经把链表题做了不少了,今天把剑指里的链表一次性全部刷完,为了博客的完整性,即使很简单的题目,也都记录下来了,比如这题。。
在这里插入图片描述

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

没啥好方法,大家都这么做的。。

发布了71 篇原创文章 · 获赞 20 · 访问量 4818

猜你喜欢

转载自blog.csdn.net/qq_22795223/article/details/105702730