classListNode: def__init__(self, val=0, next=None): self.val = val self.next = next
classSolution: defreverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head isNone: returnNone ans = 0 while head: if ans == 0: ans = ListNode(head.val) else: ans = ListNode(head.val, ans) head = head.next return ans