Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please implement add_back (below) SOLUTION MUST BE RECURSIVE! This method adds a new node at the end of the list (right before the back sentinel).
please implement add_back (below) SOLUTION MUST BE RECURSIVE! This method adds a new node at the end of the list (right before the back sentinel). Testing Example #1: lst = LinkedList() print(lst) lst.add_back('C') lst.add_back('B') lst.add_back('A') print(lst) Output: SLL[] SLL[C -> B -> A] class LinkedList: def __init__(self, start_list=None): """ Initializes a new linked list with front and back sentinels DO NOT CHANGE THIS METHOD IN ANY WAY """ self.head = SLNode(None) self.tail = SLNode(None) self.head.next = self.tail # populate SLL with initial values (if provided) # before using this feature, implement add_back() method if start_list is not None: for value in start_list: self.add_back(value) def __str__(self) -> str: """ Return content of singly linked list in human-readable form DO NOT CHANGE THIS METHOD IN ANY WAY """ out = 'SLL [' if self.head.next != self.tail: cur = self.head.next.next out = out + str(self.head.next.value) while cur != self.tail: out = out + ' -> ' + str(cur.value) cur = cur.next out = out + ']' return out def length(self) -> int: """ Return the length of the linked list DO NOT CHANGE THIS METHOD IN ANY WAY """ length = 0 cur = self.head while cur.next != self.tail: cur = cur.next length += 1 return length def is_empty(self) -> bool: """ Return True is list is empty, False otherwise DO NOT CHANGE THIS METHOD IN ANY WAY """ return self.head.next == self.tail # ------------------------------------------------------------------ #
def add_back(self, value: object) -> None: """ Add value to back or list
SOLUTION MUST BE RECURSIVE!
"""
python code here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started