Leetcode 刷题Day2(3)
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
# 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]:
res=[]
while head:
res.append(head.val)
head=head.next
return res[::-1]

python列表中[::-1]表示按照间隔一倒序输出
test=[1,2,3,4,5,6]
test[::-1]=[6,5,4,3,2,1]
test[::-2]=[6,4,2]
[:-1]表示从0到-1之前的数
test[:-1]=[1,2,3,4,5]