Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return "Node({})".format(self.value) __repr__ = __str__ class SortedLinkedList: ''' >>> x=SortedLinkedList() >>> x.add(8.76) >>> x.add(1) >>> x.add(1) >>> x.add(1) >>> x.add(5) >>> x.add(3) >>> x.add(-7.5) >>> x.add(4) >>> x.add(9.78) >>> x.add(4) >>> x Head:Node(-7.5) Tail:Node(9.78) List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78 >>> x.replicate() Head:Node(-7.5) Tail:Node(9.78) List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 3 -> 3 -> 4 -> 4 -> 4 -> 4 -> 4 -> 4 -> 4 -> 4 -> 5 -> 5 -> 5 -> 5 -> 5 -> 8.76 -> 9.78 >>> x Head:Node(-7.5) Tail:Node(9.78) List:-7.5 -> 1 -> 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> 8.76 -> 9.78 >>> x.removeDuplicates() >>> x Head:Node(-7.5) Tail:Node(9.78) List:-7.5 -> 1 -> 3 -> 4 -> 5 -> 8.76 -> 9.78 ''' def __init__(self): # You are not allowed to modify the constructor self.head=None self.tail=None def __str__(self): # You are not allowed to modify this method temp=self.head out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' -> '.join(out) return 'Head:{} Tail:{} List:{}'.format(self.head,self.tail,out) __repr__=__str__ def isEmpty(self): return self.head == None def __len__(self): count=0 current=self.head while current: current=current.next count+=1 return countdef replicate(self): # --- YOUR CODE STARTS HERE passdef removeDuplicates(self): # --- YOUR CODE STARTS HERE pass

question:

image text in transcribedimage text in transcribed
replicate(sel (2.5 points) Returns a new SortedLinkedList object where each element of the linked list appears its node's value number of times (3 > 1 results in 3 > 3 -> 3 -> 1). Negative numbers and oats are ignored, they are not repeated but should be included in the list. For 0, the node is added in the new list only once. Method returns None if the list is empty. Hint: Using the add method from above could be very useful here! Oulput SortedLinkedList A new linked list object that repeats positive integer values value times. It does not modify the original list N None keyword is returned if the original list is empty removeDuplicates(self) (2.5 points) Removes any duplicate nodes from the list, so it modifies the original list. This method must traverse the list only once. It modifies the original list. Output None Nothing is returned when the method completes the work

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions