Java LinkedList listIterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, listIterator() returns a ListIterator, an iterator for lists that can move in both directions. Besides next() and remove(), it has hasPrevious(), previous(), add(e) and set(e).

Think of the iterator as a cursor that sits between two elements. next() jumps over the element on the right, and previous() jumps over the element on the left. The version listIterator(index) places the cursor before the element at index.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Use the no-argument form to start at the front, or give an index.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
ListIterator<String> it = list.listIterator();       // cursor before "A"
ListIterator<String> end = list.listIterator(list.size());  // cursor after "C"
String s = end.previous();                           // "C"
CallWhat it does
list.listIterator()Starts at the front (index 0).
list.listIterator(index)Starts before the element at index. Valid from 0 to size().
it.previous()Returns the element on the left and moves back.
it.add(e) / it.set(e)Insert a new element at the cursor, or replace the last returned element.

nextIndex() is the index that next() would return, and previousIndex() is one less.

Library declarations
Java
// java.util.LinkedList
public ListIterator<E> listIterator()
public ListIterator<E> listIterator(int index)

Parameters

ParameterDescription
index (int)Optional. The index of the first element that next() will return. Must be between 0 and size().

Return Value

Return ValueDescription
ListIterator<E>A list iterator positioned before the element at the given index.

Compare with Similar Methods

Ways to walk a list:

MethodHow it differs
iterator()Moves forward only and cannot add or replace.
descendingIterator()Walks backward without needing to jump to the end first.
set(i, e)Replaces by index, which is slow in a loop on a LinkedList.
ArrayList.listIterator()The same method on an array-backed list.

Java Examples

Example 1 — Walk Forward, Then Backward

Move to the end of a list with next, then come back with previous.

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorBoth {
    public static void main(String[] args) {
        LinkedList<String> steps = new LinkedList<>(Arrays.asList("one", "two", "three"));
        ListIterator<String> it = steps.listIterator();

        System.out.println("forward:");
        while (it.hasNext()) {
            System.out.println("  " + it.nextIndex() + " " + it.next());
        }

        System.out.println("backward:");
        while (it.hasPrevious()) {
            System.out.println("  " + it.previousIndex() + " " + it.previous());
        }
    }
}

How It Works

  1. The cursor starts at the front. nextIndex() is 0 and hasPrevious() is false.
  2. Going forward. Each next() jumps over one element, so the indexes 0, 1 and 2 are printed with one, two and three.
  3. The cursor is now at the end. It sits after "three", so hasNext() is false and hasPrevious() is true.
  4. Going backward. previous() returns three, two and one, and previousIndex() is the index of the element being returned.

Example 2 — Replace and Insert While Walking

Upper-case one word with set, and insert a new word after another with add.

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorEdit {
    public static void main(String[] args) {
        LinkedList<String> words = new LinkedList<>(Arrays.asList("red", "green", "blue"));
        ListIterator<String> it = words.listIterator();

        while (it.hasNext()) {
            String w = it.next();
            if (w.equals("green")) {
                it.set("GREEN");
                it.add("yellow");
            }
        }

        System.out.println("words: " + words);
        System.out.println("size: " + words.size());
    }
}

How It Works

  1. Walk until "green". The loop reads red first, and nothing happens, then it reads green.
  2. set replaces the last returned element. set("GREEN") changes green in place.
  3. add inserts at the cursor. The cursor is right after GREEN, so "yellow" goes between GREEN and blue, and the iterator moves past it.
  4. The loop continues normally. It then reads blue, and the final list has four elements.

Example 3 — Start Walking from the Middle or the End

Use listIterator(index) to begin at a chosen position, and also try a bad index.

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorStart {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(10, 20, 30, 40, 50));

        ListIterator<Integer> mid = nums.listIterator(2);
        System.out.println("next from 2: " + mid.next());

        ListIterator<Integer> end = nums.listIterator(nums.size());
        System.out.print("reversed:");
        while (end.hasPrevious()) {
            System.out.print(" " + end.previous());
        }
        System.out.println();

        try {
            nums.listIterator(6);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("listIterator(6) -> IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Cursor before index 2. listIterator(2) places the cursor in front of 30.
  2. next returns 30. That is the element at index 2.
  3. Starting at size(). listIterator(nums.size()) puts the cursor after the last element, and previous() walks back from there, giving 50, 40, 30, 20, 10.
  4. The valid range is 0 to size(). The list has five elements, so index 6 is too large and throws IndexOutOfBoundsException.

Frequently Asked Questions

listIterator can move backward as well as forward, and it can add and replace elements. iterator can only move forward and remove.
Create listIterator(list.size()) and loop while hasPrevious(), calling previous(). Or use descendingIterator().
Call it.add(element) on a ListIterator. The element is inserted at the cursor, and the next call to next() is not affected.
Call it.set(element) right after next() or previous(). It replaces the element that was just returned.
The index of the element that the next call to next() would return. At the end of the list it returns size().

Next: offer()

Add an element to the tail of the queue and return true.

offer() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful