leetcode题目链接
def reverseList(head): cur = head pre = None while cur != None: temp = cur.next # 保存一下cur的下一个节点,用于更新 cur.next = pre # 反转 # 更新pre、cur指针 pre = cur cur = temp return pre # 返回pre而不是返回cur
def reverseList(head): def reverse(pre,cur): if cur==None: # 递归出口 return pre temp = cur.next # 保存一下cur的下一个节点,用于更新 cur.next = pre # 反转 reverse(cur,temp) # 更新pre和cur指针 return reverse(None,head)