剑指 Offer 06. 从尾到头打印链表
程序员文章站
2022-06-17 17:21:13
...
剑指 Offer 06. 从尾到头打印链表
链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
数组简单应用,依次存储链表各个节点的值,然后反转就好了
# 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]