采用递归
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
pre = None
while cur:
temp = cur.next # 保存下一轮循环的节点
cur.next = pre # 将当前节点 cur 的指针指向上一个节点 pre
pre = cur
cur = temp
return pre