My python solution


  • 3
    L
    class Solution(object):
    def reverse_recursively(self, head, new_head):
        if not head:
            return new_head
        nxt = head.next
        head.next = new_head
        return self.reverse_recursively(nxt, head)
    
    def reverseList(self, head):
        # recursively
        return self.reverse_recursively(head, None)
    
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        new_head = None
        while head:
            nxt = head.next
            head.next = new_head
            new_head = head
            head = nxt
        return new_head

Log in to reply
 

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