Doing this with iterators is a weird way, as the Collections.swap method (which is probably the easiest way to get what you need to achieve) specifically asks for indexes. So using those in the first place would be the way to go. Nonetheless, you can obviously also do this with iterators. This could look like the following:
public static <T> List<T> reverse(List<T> input)
{
ListIterator<T> it = input.listIterator();
ListIterator<T> itR = input.listIterator(input.size());
while (it.nextIndex() - itR.previousIndex() < 0)
{
Collections.swap(input, it.nextIndex(), itR.previousIndex());
it.next();
itR.previous();
}
return input;
}
You begin by setting up your iterators. One starts at the beginning of the list, one at the end.
Then you loop as long as it.nextIndex() - itR.previousIndex() < 0. This is basically moving both iterators to the center of the List without them crossing so you don't swap back already swapped elements.
Finally you simply swap the elements at the corresponding indexes and move the iterators.
Update
As you specified that you can't directly use Collections.swap (for whatever reason) you can obviously also reinvent the wheel and just rewrite what the method does.
public static <T> List<T> reverse(List<T> input)
{
ListIterator<T> it = input.listIterator();
ListIterator<T> itR = input.listIterator(input.size());
while (it.nextIndex() - itR.previousIndex() < 0)
{
T temp = input.get(it.nextIndex());
input.set(it.nextIndex(), input.get(itR.previousIndex()));
input.set(itR.previousIndex(), temp);
it.next();
itR.previous();
}
return input;
}
Does the exact same just without using the (honestly in all cases preferred) Collections.swap method.