Python - Define a new class named MyLinkedList that extends LinkedList with the following three methods:
def addAll(self, otherList):
which adds the elements in otherList to this list, and returns True if this list changed as a result of the call.
def removeAll(self, otherList):
which removes all the elements in otherList from this list and returns True if this list changed as a result of the call.
def retainAll(self, otherList):
which retains the elements in this list that are also in otherList and returns True if this list changed as a result of the call.
I have this so far and need to modify it, any help?
class LinkedList:
# Write the code for LinkedList
def main():
lista = input("Enter list1: ").split()
listb = input("Enter list2: ").split()
list1 = LinkedList()
list2 = LinkedList()
for s in lista:
list1.add(s)
for s in listb:
list2.add(s)
list1.addAll(list2)
print("After list1.addAll(list2), list1 is", list1)
list1 = LinkedList()
for s in lista:
list1.add(s)
list1.removeAll(list2)
print("After list1.removeAll(list2), list1 is", list1)
list1 = LinkedList()
for s in lista:
list1.add(s)
list1.retainAll(list2)
print("After list1.retainAll(list2), list1 is", list1)
main()
Program:
MyLinkedList.py
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __repr__(self):
return repr(self.data)
class LinkedList:
def __init__(self):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
def __repr__(self):
"""
Return a string representation of the list.
Takes O(n) time.
"""
nodes = []
curr = self.head
while curr:
nodes.append(repr(curr))
curr = curr.next
return '[' + ', '.join(nodes) + ']'
def add(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
if not self.head:
self.head = ListNode(data=data)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = ListNode(data=data)
def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
# Find the element and keep a
# reference to the element preceding it
curr = self.head
prev = None
while curr and curr.data != key:
prev = curr
curr = curr.next
# Unlink it from the list
if prev is None:
self.head = curr.next
elif curr:
prev.next = curr.next
curr.next = None
def addAll(self, otherList):
"""
adds the elements in otherList to this list, and
returns True if this list changed as a result of the call
"""
curr = otherList.head
while curr:
curr = curr.next
self.add(curr)
return True
def removeAll(self, otherList):
"""
removes all the elements in otherList from this list and
returns True if this list changed as a result of the call
"""
if not otherList.head:
return
curr = otherList.head
while curr:
curr = curr.next
self.remove(curr)
return True
def retainAll(self, otherList):
"""
retains the elements in this list that are also in otherList
and
returns True if this list changed as a result of the call.
"""
if not otherList.head:
return
curr = otherList.head
while curr:
curr = curr.next
self.add(curr)
otherList.remove(curr)
return True
# MyLinkedList Implementation
# class MyLinkedList(LinkedList):
# def add(self, data):
# """
# Insert a new element at the end of the list.
# Takes O(n) time.
# """
# if not self.head:
# self.head = ListNode(data=data)
# return
# curr = self.head
# while curr.next:
# curr = curr.next
# curr.next = ListNode(data=data)
# def remove(self, key):
# """
# Remove the first occurrence of `key` in the list.
# Takes O(n) time.
# """
# # Find the element and keep a
# # reference to the element preceding it
# curr = self.head
# prev = None
# while curr and curr.data != key:
# prev = curr
# curr = curr.next
# # Unlink it from the list
# if prev is None:
# self.head = curr.next
# elif curr:
# prev.next = curr.next
# curr.next = None
# def addAll(self, otherList):
# """
# adds the elements in otherList to this list, and
# returns True if this list changed as a result of the call
# """
# curr = self.head
# while curr:
# curr = curr.next
# self.add(curr)
# return True
# def removeAll(self, otherList):
# """
# removes all the elements in otherList from this list and
# returns True if this list changed as a result of the call
# """
# curr = self.head
# while curr:
# curr = curr.next
# self.remove(curr)
# def retainAll(self, otherList):
# """
# retains the elements in this list that are also in otherList
and
# returns True if this list changed as a result of the call.
# """
# curr = otherList.head
# while curr:
# curr = curr.next
# self.add(curr)
# otherList.remove(curr)
def main():
lista = input("Enter list1: ").split()
listb = input("Enter list2: ").split()
list1 = LinkedList()
list2 = LinkedList()
for s in lista:
list1.add(s)
for s in listb:
list2.add(s)
list1.addAll(list2)
print("After list1.addAll(list2), list1 is", list1)
list1 = LinkedList()
for s in lista:
list1.add(s)
list1.removeAll(list2)
print("After list1.removeAll(list2), list1 is", list1)
list1 = LinkedList()
for s in lista:
list1.add(s)
list1.retainAll(list2)
print("After list1.retainAll(list2), list1 is", list1)
main()




Output:
Enter list1: 1 2 3
Enter list2: 4 5 6
After list1.addAll(list2), list1 is ['1', '2', '3', '5', '6',
None]
After list1.removeAll(list2), list1 is ['1', '2', '3']
After list1.retainAll(list2), list1 is ['1', '5', '6', None]
After list1.retainAll(list2), list1 is ['1', '5', '6', None, '2',
'5', '6', None]
After list1.retainAll(list2), list1 is ['1', '5', '6', None, '2',
'5', '6', None, '3', '5', '6', None]

Hope this helps!
Please let me know if any changes needed.
Thank you!
Python - Define a new class named MyLinkedList that extends LinkedList with the following three methods:...