题目
代码
执行用时:116 ms, 在所有 Python3 提交中击败了12.70% 的用户
内存消耗:25.8 MB, 在所有 Python3 提交中击败了10.26% 的用户
通过测试用例:24 / 24
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
return self.reversePrint(head.next) + [head.val] if head else []
【方法2】
执行用时:40 ms, 在所有 Python3 提交中击败了67.22% 的用户
内存消耗:16.7 MB, 在所有 Python3 提交中击败了29.75% 的用户
通过测试用例:24 / 24
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
ans=[]
while head:
ans.append(head.val)
head=head.next
return ans[::-1]