# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution:defreverseList(self,head: Optional[ListNode]) -> Optional[ListNode]: pre, cur =None, headwhile cur: nxt = cur.next cur.next = pre pre = cur cur = nxtreturn pre