欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

剑指 Offer 06. 从尾到头打印链表

程序员文章站 2022-06-17 17:21:13
...

剑指 Offer 06. 从尾到头打印链表

链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

剑指 Offer 06. 从尾到头打印链表

 

数组简单应用,依次存储链表各个节点的值,然后反转就好了

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reversePrint(self, head: ListNode) -> List[int]:
        stack = []
        while head:
            stack.append(head.val)
            head = head.next
        return stack[::-1]