题目描述
https://leetcode-cn.com/problems/reverse-linked-list/
思路题解
递归
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if not head or not head.next:return head
p=self.reverseList(head.next)
head.next.next=head
head.next=None
return p
迭代(三指针)
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre=None
while head:
end=head.next
head.next=pre
pre=head
return pre