I have been working on a recursively defined list class in Python and I’m having trouble coding a reverse() method to function recursively. Here’s the basis for the class.
class RecList:
def __init__(self):
self._head = None
self._rest = None
The base case would be self._head, being the first entry in a list, followed by the recursive case which is essentially another list containing its own self._head to start, then defined recursively. This proceeds all the way down to the bottom level where both self._head and self._rest = None. Is there a simple way to code a reverse method for a list defined like this?
Try this: