Reverse Linked List in Python


  • 0
    J

    class Solution(object):
    def reverseList(self, head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    return self._reverse(head)

    def _reverse(self, node):
        if node is None or node.next is None:
            return node
        
        head = self._reverse(node.next)
        node.next.next = node
        node.next = None
        
        return head

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.