Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part 1 - Recursion on a Linked List Linked lists can be thought of as recursive structures. Every linked list is either: the empty list,
Part Recursion on a Linked List
Linked lists can be thought of as recursive structures. Every linked list is either:
the empty list, or
a node that links to a linked list
This makes them a great playground for implementing various types of recursive functions. We provide starter code that demonstrates a few methods of recursion in a LinkedList, and you will implement a few more.
Starter Code
The LinkedList class is already fully implemented. It is essentially a wrapper the LLNode class does all the interesting recursion.
In LLNode, we have implemented:
gettail
and gettail show various examples of recursive LLNode functions. Study them to understand how they work, then implement the following LLNode methods. Note that we have listed the parameters you should use this is a great hint into how you can efficiently structure your code. Do not change these.
addlast self item
recursively call on linked node until you hit the tail, at which point you should add a new node
we are not keeping track of a tail node, so this has running time
sumalself
recursively add all items in linked list and return the sum
containsself item
recursively call on linked node until you either:
find the item and return True, or
hit the end of the list without finding the item and return False
running time
removeall self item
recursively removes all nodes holding item in linked list
running time
reverseself
recursively reverses the linked list contained in self. link unless it is the tail node
after reversing the linked list in self.link, puts itself at the end of the reversed list
this method should return the new head of the list ie the old tail
running time
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