Question: In the file you downloaded ( or opened ) at the beginning of this activity, add the method duplicate ( self , item ) to
In the file you downloaded or opened at the beginning of this activity, add the method duplicateself item to the LinkedList class. This method will modify the original list such that if there is a node that has a value that is equal to item, that node will be duplicated do not duplicate the added nodes Note that you should be mutating the original link list; so you are not allowed to create new instances of the LinkedList class, you are only allowed to create Nodes and update links. Method returns None. Submit your code in the R Recitation Activity before the end of your recitation.
You can test your code with this small doctest:
lst LinkedList
lstadd
lstadd
lstadd
lst
Head:Node
Tail:Node
List:
lstduplicate
lst
Head:Node
Tail:Node
List:
lstduplicate
lst
Head:Node
Tail:Node
List:
lstadd
lstduplicate
lst
Head:Node
Tail:Node
List:
lstduplicate
lst
Head:Node
Tail:Node
List:
class Node:
def initself value:
self.value value
self.next None
def strself:
return "Nodeformatselfvalue
reprstr
class LinkedList:
def initself:
self.headNone
self.tailNone
def strself:
tempself.head
out
while temp:
out.appendstrtempvalue
temptemp.next
outjoinout
return 'Head:
Tail:
List:formatselfhead,self.tail,out
reprstr
def isEmptyself:
return self.headNone
def lenself:
currentself.head
count
while current is not None:
count
current current.next
return count
def addself value:
newNodeNodevalue
if self.isEmpty:
self.headnewNode
self.tailnewNode
else:
newNode.nextself.head
self.headnewNode
def duplicateself item:
# YOUR CODE STARTS HERE
pass
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
