I got a task where I have to reverse a List with one or more ListIterators
. I am not allowed to use the Method collections.reverse()
or other Methods like that. I am also not allowed to make a new field. I can't just use a new List as well. The input has to be changed!
This is what I got so far. But the input isn't changed:
public static <T> List<T> reverse(List <T> input) {
ListIterator <T> listIterator2 = input.listIterator(input.size());
ListIterator <T> listIterator = input.listIterator(input.size());
int u = input.size()-1;
while(listIterator.hasNext()) {
listIterator.next();
while (listIterator2.hasPrevious()) {
listIterator.set(input.get(u));
listIterator2.previous();
u--;
}
}
return input;
}