I am looking to do it with Python. And I don\'t want to just print it in reverse, but actually reverse the given nodes. I have seen it done in other languages but had trouble fi
def reverse_list(self,node):
if node is None:
return //list is empty
elif node.next is None:
self.head = node // changing the head to last node after reversal
return
else:
reverse_list(node.next) //call the function recursively until the end of the list
node.next.next = node //reverse the link
node.next = None
Very similar to poke's solution, but I prefer to have the base case first:
def reverse(head, reversed=None):
if head is None:
return reversed
new_head = head.next
head.next = reversed
new_reversed = head
return reverse(new_head, new_reversed)