The following routine removes the first half of the list passed as a parameter: public static void
Fantastic news! We've Found the answer you've been seeking!
Question:
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?
Related Book For
Data Structures and Algorithm Analysis in Java
ISBN: 978-0132576277
3rd edition
Authors: Mark A. Weiss
Posted Date: