206. 反转链表
迭代
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while(cur):
next = cur.next
cur.next = pre
pre = cur
cur = next
return pre
函数的递归
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
p = self.reverseList(head.next)
head.next.next = head
head.next = None
return p