Python help; Instructions and test cases are posted. Below is what I have for ad
ID: 3740994 • Letter: P
Question
Python help; Instructions and test cases are posted.
Below is what I have for add(item), but it is returning '8' instead of '58' for ordered_ll.tail.
def add(self, value):
#write your code here
s=self.head
maxVal=0
for i in range(self.count):
if maxVal<s.getValue():
maxVal=s.getValue()
s=s.getNext()
self.tail=maxVal
temp = Node(value)
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getValue() > value:
stop = True
else:
previous = current
current = current.getNext()
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
self.count+=1
Explanation / Answer
try the below code:
i hope this works.
def add(self,item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.