Question
PYTHON:3: Write a LinkedList class that has recursive implementations of the add, display, and remove methods described in the lesson. It should also have recursive
PYTHON:3: 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
Need to have recursive functions for add, display, remove as well. Same with contains, insert, reverse. below are functions given but need to be translated to recursive. Need help. I'm stuck. No loops allowed in LinkedList as well.
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
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