65.3k views
4 votes
In the linked list version of the Bag class an instance variable manyNodes is used to keep track of how long the linked list is. Why not just make a call to the IntNode method listLength()?

a. The listLength() method is )(n) and the alternative is O(1)
b. The listLength() method is private
c. The listLength() method results in an infinite loop for circular lists
d. The listLength() method works only for lists of integers

User ArunM
by
7.5k points

1 Answer

6 votes

Final answer:

The linked list version of the Bag class uses the manyNodes variable to keep track of the length of the linked list instead of calling the listLength() method.

Step-by-step explanation:

The linked list version of the Bag class uses an instance variable called manyNodes to keep track of the length of the linked list. Instead of making a call to the listLength() method, it is more efficient to use O(1) constant time using manyNodes. This is because the listLength() method has a time complexity of O(n), which means it grows linearly with the number of elements in the list.

User Mohan Raj B
by
7.7k points