educative.io

Alternative approach

why not this - it doesn’t mutate the input which seems to better fit the instructions and generally it’s considered a bad practice to mutate function inputs

def reverse(lst):
  # Write your code here
  node = lst.get_head()
  reverse = LinkedList()
  while node:
    reverse.insert_at_head(node.data)
    node = node.next_element
    
  return reverse

also for in-place mutation I feel like this is a bit less confusing

def reverse(lst):
  node = lst.get_head()
  while node and node.next_element:
    lst.insert_at_head(node.next_element.data)
    node.next_element = node.next_element.next_element
  return lst