输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
如输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]
0 <= 链表长度 <= 10000
class ListNode:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def reverse_print(head):
stack = []
current = head
while current:
stack.append(current.value)
current = current.next
return stack[::-1]
# 创建链表 1 -> 2 -> 3
head = ListNode(1, ListNode(2, ListNode(3)))
# 调用函数并打印结果
print(reverse_print(head))