欢迎光临散文网 会员登陆 & 注册

Leetcode 刷题Day2(3)

2022-04-02 13:47 作者:我喜欢喝一点点  | 我要投稿

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。


# 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] 

Leetcode 刷题Day2(3)的评论 (共 条)

分享到微博请遵守国家法律