In python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class that contains only two data members and no methods (besides init), so there's not a need for encapsulation. Another way of putting it is that there's no need to separate interface from implementation because there is no interface (public methods of the class). You may use default arguments and/or helper functions. Your recursive functions must not: use any loops use any variables declared outside of the function use any mutable default arguments (see the Code Style Requirements) Here's an example of how a recursive version of the display() method from the lesson could be written: def rec_display(self, a_node): """recursive display method""" if a_node is None: return print(a_node.data, end=" ") self.rec_display(a_node.next) def display(self): """recursive display helper method""" self.rec_display(self._head)
In python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds).
It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list.
The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it).
As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class that contains only two data members and no methods (besides init), so there's not a need for encapsulation. Another way of putting it is that there's no need to separate interface from implementation because there is no interface (public methods of the class).
You may use default arguments and/or helper functions.
Your recursive functions must not:
- use any loops
- use any variables declared outside of the function
- use any mutable default arguments (see the Code Style Requirements)
Here's an example of how a recursive version of the display() method from the lesson could be written:
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images