I want to reverse a java.util.LinkedList<Integer> using the available methods.
Looking in the methods provided and the Iterators I couldn’t see an option other than the following:
int i = list.size();
int pos = 0;
while(i-- > 1){
Integer n = list.removeLast();
list.add(pos++, n);
}
But surely there must be a better way. I mean it is not a good idea to modify a list outside of an iterator, but I couldn’t see how I could use one here without having to create a new list.
Is there a better way?
Use import java.util.Collections;