mq030.反转链表 Leetcode linkedList recursion Dec 07, 2020 题目链接 啊要命了,很快就写出了用迭代实现的方法,但是卡在了用递归反转,然后就跑去玩了很久,赶快跑过来把别人的代码先抄一下否则今天就废了。这个算法的时间复杂度是O(N),空间复杂度是O(1)。 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverseList1(self, head: ListNode) -> ListNode: prev = None while head is not None: tmp = head.next head.next = prev prev = head head = tmp return prev def reverseList2(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 PREVIOUSmq029.合并两个有序链表NEXTmq031.有效的括号