Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What can a ListIterator do that an Iterator cannot, and where is its cursor positioned?

A ListIterator can move in both directions with hasPrevious() and previous(), report positions with nextIndex() and previousIndex(), replace the last returned element with set() and insert with add(), and it can start at any position with list.listIterator(index). Its cursor never sits on an element: it sits in the gap between two elements, and add() inserts at that gap, before the element the next call to next() would return.

Why this question is asked

Most of the time we iterate lists with an enhanced for loop and never think about the iterator underneath. But when we need to edit a list while walking it, such as replacing tokens, inserting separators or stepping back after a lookahead, ListIterator is the tool built for it, and using it correctly depends on understanding where its cursor is. Interviewers ask this to see whether we know the API beyond Iterator and whether we can reason about cursor positions precisely. In this answer, we will recap Iterator, picture the ListIterator cursor, walk in both directions, and then edit the list with set, add and remove, including the rules that make those calls fail.

1A Quick Recap of Iterator

An Iterator walks a collection forward, one element at a time. It has three methods: hasNext() tells whether another element exists, next() returns it and moves forward, and remove() deletes the element that next() just returned. That is all. It cannot go back, cannot tell us where it is, and cannot add or replace anything.

ListIterator extends Iterator and is available only on lists, because only lists have positions. We get one from list.listIterator() to start at the beginning, or from list.listIterator(index) to start at a given position.

2Where the Cursor Sits

The key idea is that the cursor of a ListIterator lies between elements, never on one. A list of three elements has four cursor positions: before the first, between the first and second, between the second and third, and after the last. next() returns the element to the right of the cursor and moves the cursor past it. previous() returns the element to the left and moves the cursor back past it.

nextIndex() is the index of the element that next() would return, and previousIndex() is the index of the element that previous() would return. So previousIndex() is always nextIndex() - 1, which is -1 at the start and size() - 1 at the end.

Cursor positions in ["a", "b", "c"]
Cursor positionnextIndex()previousIndex()next() would return
before "a"0-1"a"
between "a" and "b"10"b"
between "b" and "c"21"c"
after "c"32nothing: NoSuchElementException
A list of n elements has n + 1 cursor positions.

3Walking Forwards and Backwards

Here is the cursor in action. Notice that calling next() and then previous() returns the same element twice, because both calls cross the same gap in opposite directions.

Java
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
ListIterator<String> it = list.listIterator();
it.nextIndex(); // 0
it.next(); // "a"
it.next(); // "b"
it.previous(); // "b" again, the cursor moved back over it
it.previousIndex(); // 0
it.hasPrevious(); // true
ListIterator<String> fromEnd = list.listIterator(list.size());
while (fromEnd.hasPrevious()) {
System.out.print(fromEnd.previous()); // prints cba
}

Starting at list.size() is the standard way to walk a list backwards: the cursor begins after the last element, and previous() walks back to the front. This works well on LinkedList too, where get(i) in a loop would cost O(n) per call, while the iterator moves one link at a time.

4Editing While Walking: set, add and remove

set(e) replaces the element that was last returned by next() or previous(). remove() deletes that same element. add(e) is different: it does not touch the last returned element, it inserts e at the cursor, so the new element lands before whatever next() would return and after whatever previous() would return. After add(), next() is unaffected and previous() would return the new element.

Java
List<String> words = new ArrayList<>(List.of("one", "TWO", "three"));
ListIterator<String> it = words.listIterator();
while (it.hasNext()) {
String w = it.next();
if (w.equals("TWO")) {
it.set("two"); // [one, two, three]
it.add("and-a-half"); // [one, two, and-a-half, three]
}
}
System.out.println(words); // [one, two, and-a-half, three]

Notice that the loop does not visit "and-a-half". It was inserted before the cursor, so the next call to next() returns "three". That is exactly what we usually want: inserting while walking forward never makes the loop process its own insertions, so it cannot loop forever.

5The Rules That Make set and remove Fail

set() and remove() both need a last returned element, and that element must still be valid. Two things invalidate it: a call to add(), because the cursor's neighbourhood has changed, and a call to remove(), because the element is gone. In either case, set() or remove() throws IllegalStateException until we call next() or previous() again. Calling either of them before the first next() fails the same way.

Java
ListIterator<String> it = new ArrayList<>(List.of("a", "b")).listIterator();
it.set("x"); // IllegalStateException: nothing returned yet
it.next(); // "a"
it.remove(); // list is now [b]
it.remove(); // IllegalStateException: "a" is already gone
it.next(); // "b"
it.add("c"); // list is [b, c], cursor after "c"
it.set("z"); // IllegalStateException: add was called since next()

The reason for these rules is that set and remove are defined relative to the last element crossed by the cursor. After an add or a remove, there is no single element that clearly fits that description, so the iterator refuses rather than guessing. Remember also that all of this must go through the iterator: changing the list directly while a ListIterator is open makes the next iterator call throw ConcurrentModificationException.

6Follow-up questions

Does every list support set and add through ListIterator?

No. The operations are optional. List.of() returns an immutable list whose iterator throws UnsupportedOperationException for set, add and remove. Arrays.asList() supports set, because it writes through to the array, but not add or remove, because the array cannot change size.

What does listIterator(index) throw for a bad index?

It throws IndexOutOfBoundsException if the index is below 0 or above size(). Note that size() itself is allowed, because the position after the last element is a valid cursor position.

When would we choose a ListIterator over index-based loops on an ArrayList?

On an ArrayList both are fast, so the iterator is mainly clearer and safer when we insert or remove while walking, since we do not have to adjust an index by hand. On a LinkedList the iterator is much faster, because add and remove at the cursor are O(1) while index-based operations walk the list each time.

7Conclusion

In this answer, we saw that ListIterator adds what Iterator lacks: backward movement with hasPrevious and previous, positions with nextIndex and previousIndex, set to replace the last returned element, add to insert, and a starting position through listIterator(index). Its cursor sits between elements, so a list of n elements has n + 1 positions and add inserts at the gap, before the element next() would return. set and remove act on the last returned element and throw IllegalStateException if add or remove has been called since.

What to say in the interviewAbout 90 seconds aloud

Open with

A plain Iterator can only go forward and remove the element it just returned. ListIterator, which you only get from a List, adds backward movement with hasPrevious and previous, position information with nextIndex and previousIndex, set to replace the element last returned, and add to insert an element.

Then, if they want more

You can also start anywhere with list.listIterator(index); passing list.size() is the usual way to walk a list backwards. The important detail is the cursor position.

It never sits on an element, it sits between elements, so a list of three elements has four positions. next returns the element after the cursor and previous returns the one before it, which is why calling next then previous gives you the same element twice. nextIndex is the index next would return, and previousIndex is always one less. add inserts at the cursor, before the element that next would return, so if you add while walking forward the loop does not visit the new element. set and remove act on the last element returned by next or previous, and they throw IllegalStateException if you haven't called next or previous yet, or if you've called add or remove since then.

And as with any iterator, if the list is changed directly rather than through the iterator, the next call fails with ConcurrentModificationException.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What can a ListIterator do that an Iterator cannot, and where is its cursor positioned?": https://scrivio-interview-library.netlify.app/java/iteration/d67553d860/