小弟最近在学习算法和数据结构,奈何经常搞不懂递归的问题,也查阅了不少资料,但还是没搞懂用递归去反转链表
所以发了这个贴请教一下 v2 上面的大佬,谢谢大家啦
下面是从 leetcode 看到的 solution
class Solution:
def reverseList(self, head):
if not head or not head.next:
return head
new_head = self.reverseList(head.next)
next_node = head.next # head -> next_node
next_node.next = head # head <- next_node
head.next = None # [x] <- head <- next_node
return new_head
不能理解的地方是,返回最后一个的节点指针时候这三行代码的意思
next_node = head.next
next_node.next = head
head.next = None

