反转单链表

迭代实现:

def reverse_linked_list_iteration(node):
    if not node:
        return node
    pre_node = None
    while node.next:
        next = node.next
        node.next = pre_node
        pre_node = node
        node = next
    node.next = pre_node
    return node

递归实现:

def reverse_linked_list_recursion(node):
    if not node or not node.next:
        return node
    new_head = reverse_linked_list_recursion(node.next)
    node.next.next = node
    node.next = None
    return new_head