# Definition for singly-linked list. classListNode: def__init__(self, x): self.val = x self.next = None
classSolution: defreversePrint(self, head): pre = head result = [] if head isNone: return result while pre: result.append(pre.val) pre = pre.next # result.reverse() return result[::-1] # 速度比 result.reverse() 快且节省内存
n1 = [1,2,3,4,5,6]
defgenerate_node(n1): head = ListNode(-1) pre = head for i in n1: pre.next = ListNode(i) pre = pre.next return head.next