【Python】【难度:简单】Leetcode 面试题06. 从尾到头打印链表

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

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]
 

限制:

0 <= 链表长度 <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

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

class Solution(object):
    def reversePrint(self, head):
        """
        :type head: ListNode
        :rtype: List[int]
        """
        res=[]
        while head:
            res.append(head.val)
            head=head.next
        return res[::-1]

执行结果:

通过

显示详情

执行用时 :32 ms, 在所有 Python 提交中击败了58.20%的用户

内存消耗 :16.3 MB, 在所有 Python 提交中击败了100.00%的用户

原创文章 105 获赞 0 访问量 1664

猜你喜欢

转载自blog.csdn.net/thomashhs12/article/details/106051622