Java LinkedList listIterator() method
Syntax and Examples

The listIterator() method in Java's LinkedList class is a powerful tool for iterating over the list elements. Specifically, `listIterator(int index)` lets you create a list iterator that starts at a specified position within the linked list. This is particularly useful when you need to iterate from a point other than the beginning of the list.

Syntax

public ListIterator<E> listIterator(int index)

Parameters

Parameter Description
index The starting index for the list iterator. This must be a valid index within the LinkedList (i.e., between 0 and size(), inclusive).

Return Value

The method returns an instance of the ListIterator<E> interface, which allows you to traverse the list forward or backward.

Examples

Iterating from a Middle Index

This example demonstrates creating a list iterator starting at index 2 of a linked list and then using it to iterate through the remaining elements.

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        list.add("Date");
        list.add("Fig");

        ListIterator<String> iterator = list.listIterator(2); // Start at index 2 (Cherry)

        System.out.println("Iterating from index 2:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
Iterating from index 2:
Cherry
Date
Fig

Explanation: We create a LinkedList containing some fruits. Then, we initialize a ListIterator starting at index 2. The loop iterates through the remaining elements (Cherry, Date, and Fig) using the `next()` method of the iterator.

Modifying the list during iteration

This example shows how to use the list iterator to modify the LinkedList while iterating, starting from a specific index.

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorModifyExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        list.add("Date");

        ListIterator<String> iterator = list.listIterator(1); // Start at index 1 (Banana)

        System.out.println("Original List: " + list);

        iterator.set("Mango");  // Replace Banana with Mango
        iterator.add("Grape");   // Insert Grape after Mango
        iterator.previous();      // Go back to the previous element (Mango)
        iterator.remove();       // Remove Mango

        System.out.println("Modified List: " + list);
    }
}
Original List: [Apple, Banana, Cherry, Date]
Modified List: [Apple, Cherry, Date]

Explanation: We start the iterator at index 1 (Banana). We use `set()` to replace "Banana" with "Mango". Then we add "Grape" after Mango. Finally, we go back to Mango and remove it.

Iterating Backwards from a Specific Index

This example demonstrates creating a list iterator starting at index 3 and then using the `previous()` method to iterate backward through the linked list.

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIteratorBackwardExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        list.add("Date");
        list.add("Fig");

        ListIterator<String> iterator = list.listIterator(3); // Start at index 3 (Date)

        System.out.println("Iterating backward from index 3:");
        while (iterator.hasPrevious()) {
            System.out.println(iterator.previous());
        }
    }
}
Iterating backward from index 3:
Date
Cherry
Banana

Explanation: The iterator is initialized at index 3 (Date). The `hasPrevious()` method checks if there are elements before the current position, and `previous()` moves the iterator back one element each time. This continues until we reach the beginning of the list.