Question: 9 test case wrong answer . sample input ? runtime error . class Node: def _ _ init _ _ ( self , value )
test case wrong answer
sample input runtime error
class Node:
def initself value:
self.value value
self.next None
class LinkedList:
def initself:
self.head None
def insertself position, value:
if position :
printerrcode
return
newnode Nodevalue
if position : #
newnode.next self.head
self.head newnode
self.printlist
return
#
length
current self.head
while current:
length
current current.next
if position length: #
printerrcode
return
#
if position length:
current self.head
while current.next:
current current.next
current.next newnode
self.printlist
return
#
currentnode self.head
index
while currentnode and index position :
currentnode currentnode.next
index
newnode.next currentnode.next
currentnode.next newnode
self.printlist
def deleteself value:
if self.head is None:
printerrcode
return
if self.head.value value: #
self.head self.head.next
self.printlist
return
currentnode self.head
while currentnode.next and currentnode.next.value value:
currentnode currentnode.next
if currentnode.next: #
currentnode.next currentnode.next.next
self.printlist
else:
printerrcode #
def reverseself:
if self.head is None:
printerrcode
return
prev None
current self.head
while current:
nextnode current.next
current.next prev
prev current
current nextnode
self.head prev
self.printlist
def findself value:
current self.head
index
while current:
if current.value value:
printindex
return
current current.next
index
printerrcode
def sublistself ps pe:
if ps or pe ps:
printerrcode
return
current self.head
index
while current and index ps:
current current.next
index
if not current: #
printerrcode
return
temphead current
while temphead and index pe:
printtemphead.value, end if temphead.next and index pe else NULL
temphead temphead.next
index
if index pe and temphead is None:
printerrcode
def countfrequencyself value:
current self.head
count
while current:
if current.value value:
count
current current.next
printcount
def sortself:
if self.head is None or self.head.next is None: #
return
values
current self.head
while current:
values.appendcurrentvalue
current current.next
values.sort
current self.head
for value in values:
current.value value
current current.next
self.printlist
def printlistself:
current self.head
while current:
printcurrentvalue, end
current current.next
printNULL
import sys
linkedlist LinkedList
while True:
command sysstdin.readlinestrip
if not command:
break
parts command.split
if parts "I":
linkedlist.insertintparts intparts
elif partsD:
linkedlist.deleteintparts
elif partsR:
linkedlist.reverse
elif partsF:
linkedlist.findintparts
elif partsS:
linkedlist.sublistintparts intparts
elif partsC:
linkedlist.countfrequencyintparts
elif partsT:
linkedlist.sort
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
