Reverse Linked List (recursive)

Reverse a singly-linked list recursively.

Examples

L = null, return null
L = 1 -> null, return 1 -> null
L = 1 -> 2 -> 3 -> null, return 3 -> 2 -> 1 -> null

class Solution(object):
  def reverse(self, head):
    return self._reverse(head)

  def _reverse(self, node, prev=None):
    if not node:
      return prev
    n = node.next
    node.next = prev
    return self._reverse(n, node)

你可能感兴趣的:(Reverse Linked List (recursive))