Question
The following routine removes the first half of the list passed as a parameter: public static void removeFirstHalf( List lst ) { int theSize =
The following routine removes the first half of the list passed as a parameter:
public static void removeFirstHalf( List> lst )
{
int theSize = lst.size( ) / 2;
for( int i = 0; i < theSize; i++ )
lst.remove( 0 );
}
a. Why is theSize saved prior to entering the for loop?
b. What is the running time of removeFirstHalf if lst is an ArrayList?
c. What is the running time of removeFirstHalf if lst is a LinkedList?
d. Does using an iterator make removeHalf faster for either type of List?
For part a, what would happen if the for loop was written as?
for (int i = 0 ; i < lst.size()/2 ; i++) {
lst.remove(0) ;
}
How much of the list would be removed? Why?
Step by Step Solution
3.41 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
a In the given routine theSize is saved prior to entering the for loop because according to the valu...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 StartedRecommended Textbook for
Data Structures and Algorithm Analysis in Java
Authors: Mark A. Weiss
3rd edition
132576279, 978-0132576277
Students also viewed these Algorithms questions
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
Question
Answered: 1 week ago
View Answer in SolutionInn App