\'\'\'Adds item to the beginning of the list only if that item is unique (i.e. i
ID: 3821728 • Letter: #
Question
'''Adds item to the beginning of the list only if that item is unique (i.e. its cargo does not match the cargo of any other node in the list)'''
while(current != None and current.cargo != cargo):
current = current.next
I have a question about linked list in python. What exactly are something.cargo and something.headdef add_unique(self, cargo):
'''Adds item to the beginning of the list only if that item is unique (i.e. its cargo does not match the cargo of any other node in the list)'''
current = self.head
while(current != None and current.cargo != cargo):
current = current.next
if current == None: node = Node(cargo, self.head) self.head = nodedef add unique (self, cargo) ''Adds item to the beginning of the list only if that item is unique (i.e its cargo does not match the cargo of any other node in the list) current self .head while (current None and current.cargo cargo) current current next if current None node Node (cargo self head) self. head node
Explanation / Answer
self.head represent the first node of the linked list.
cargo is the data node we need to add add the beginning of the list.
In function:
'''Adds item to the beginning of the list only if that item is unique (i.e. its cargo does not match the cargo of any other node in the list)'''
the above function, first it will see whether cargo does not match the cargo of any other node in the list. Then add the item named cargo at the beginning of the list and after that we make that current node to head node by
current = self.head
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.