Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . add ( self , item ) ( 2 points ) adds a new Node with value = item to the list making sure
addself item points adds a new Node with valueitem to the list making sure that the ascending order is preserved. It needs the item and returns nothing but modifies the linked list. The items in the list might not be unique, but you can assume every value will be numerical int and float You are not allowed to traverse the linked list more than once only one loop required Preconditions and Postconditions item: intfloat numerical value Returns: None
splitself points Divides the original list in half, returning two new instances of SortedLinkedList, one with the first half, and another one with the second half. If the number of nodes is odd, the extra node should go in the first half. It does not mutate the original list. Method returns None if the original list is empty. Postconditions Returns: None if the original list is empty SortedLinkedList, SortedLinkedList two linked list objects
removeDuplicatesself points Removes any duplicate nodes from the list, so it modifies the original list. This method must traverse the list only once. Values such as and are considered duplicates, so you can remove either one. Postconditions Returns: None
intersectionself other points Given two instances of SortedLinkedList, returns a new instance of SortedLinkedList that contains the nodes that appear in both lists. The intersection should not keep duplicates. You are not allowed to alter the original lists. Preconditions and Postconditions other: SortedLinkedList Returns: SortedLinkedList
class Node: # You are not allowed to modify this class
def initself value:
self.value value
self.next None
def strself:
return fNodeselfvalue
reprstr
class SortedLinkedList:
xSortedLinkedList
xadd
xadd
xadd
xadd
xadd
xadd
xadd
xadd
xadd
xadd
x
Head:Node
Tail:Node
List:
xremoveDuplicates
x
Head:Node
Tail:Node
List:
sub sub xsplit
sub
Head:Node
Tail:Node
List:
sub
Head:Node
Tail:Node
List:
x
Head:Node
Tail:Node
List:
xadd
xintersectionsub
Head:Node
Tail:Node
List:
def initself: # You are not allowed to modify the constructor
self.headNone
self.tailNone
def strself: # You are not allowed to modify this method
tempself.head
out
while temp:
out.appendstrtempvalue
temptemp.next
outjoinout
return f'Head:selfhead
Tail:selftail
List:out
reprstr
def isEmptyself:
return self.head None
def lenself:
count
currentself.head
while current:
currentcurrent.next
count
return count
def addself value:
# YOUR CODE STARTS HERE
def splitself:
# YOUR CODE STARTS HERE
def removeDuplicatesself:
# YOUR CODE STARTS HERE
def intersectionself other:
# YOUR CODE STARTS HERE
def runtests:
import doctest
doctest.testmodverboseTrue
if namemain:
runtests
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