翻转链表--递归写法

2023-04-23  本文已影响0人  HellyCla
image.png
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        # 递归方法
        
        if head == None or head.next == None:
            return head
        ret = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return ret
上一篇 下一篇

猜你喜欢

热点阅读