![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a910d46a880e3fef9cd51dcb758b5c55.png)
自己1:暴力破解法
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
num_list = list()
p = head
while p:
num_list.append(int(p.val))
p = p.next
num_list = num_list[::-1]
head_ = ListNode(-1)
p = head_
for i in num_list:
tmp = ListNode(i)
p.next = tmp
p = p.next
return head_.next
自己2:链表递归重组法
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
res = ListNode(-1)
res.next = self.merg_(head)
return res.next
def merg_(self,head):
if not head.next:
return head
head_ = self.merg_(head.next)
tail = head
tail.next = None
p = head_
while p.next:
p = p.next
p.next = tail
return head_