题目描述
输入一个链表,反转链表后,输出新链表的表头。
题目解法
递归
参考我之前在知乎发的图解文章:https://zhuanlan.zhihu.com/p/60117407
主要思路是递归到最后的节点,然后进行反转操作。
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if not pHead: return
if not pHead.next: return pHead
head = self.ReverseList(pHead.next)
pHead.next.next = pHead
pHead.next = None
return head