递归,head。next。next让head和head。next形成回环,再用head。next=null消除回环
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head == None or head.next == None:
return head
p = self.reverseList(head.next)
head.next.next = head
head.next = None
return p