Question: Write a LinkedList class that has recursive implementations of the add, display, and remove methods described in the lesson. It should also have recursive implementations

Write a LinkedList class that has recursive implementations of the add, display, and remove methods described in the lesson. It should also have recursive implementations of the contains, insert, and reverse methods described in the exercises. You may use default arguments and/or helper functions.

The file must be named: LinkedList.py

Python

class Node: def __init__(self, data): self.data = data self.next = None

class LinkedList: def __init__(self): self.head = None def add(self, val): if self.head is None: self.head = Node(val) else: current = self.head while current.next is not None: current = current.next current.next = Node(val) def display(self): current = self.head while current is not None: print(current.data, end="") current = current.next print() def remove(self, val): if self.head is None: return

if self.head.data == val: self.head = self.head.next else: current = self.head while current is not None and current.data != val: previous = current current = current.next if current is not None: previous.next = current.next def is_empty(self): return self.head is None

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!