# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def ReverseList(self , head: ListNode) -> ListNode: if head == None: return head return self.reverseListHelp(head)[0] # 返回数组当前链表反转后得到的新链表的[头结点,尾节点] def reverseListHelp(self , head: ListNode) -> List: if head.next == None: return [head , head] # 获取下一个节点翻转后的[头结点,尾节点] headAndTail = self.reverseListHelp(head.next) # 将当前节点添加到尾结点之后 headAndTail[1].next = head # 当前节点已经成为了尾结点,后面再应该有节点 head.next = None # 返回数组当前链表反转后得到的新链表的[头结点,尾节点] return [headAndTail[0] , head]