Home Java LinkedList Methods descendingIterator() Java LinkedList descendingIterator() Method Definition In Java, descendingIterator() returns an Iterator that visits the elements of the LinkedList in reverse order: last element first, first element last.
The list itself is not reversed or changed. The iterator simply walks it from the other end. Like a normal iterator, it offers hasNext(), next() and remove().
An instance method of java.util.LinkedList (since Java 1.6): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.
Syntax Use it like any other iterator.
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
Iterator<String> it = list.descendingIterator();
while (it.hasNext()) {
System.out.println(it.next()); // C, B, A
} Call What it does list.descendingIterator()An iterator from the last element to the first. list.iterator()An iterator from the first element to the last. list.listIterator(list.size())A backward walk with hasPrevious and previous. it.remove()Removes the element last returned by next().
Here next() means "the next one in reverse order". The method name looks odd, but it keeps the usual iterator loop.
Library declarations // java.util.LinkedList
public Iterator<E> descendingIterator() Parameters Parameter Description None descendingIterator() takes no parameters.
Return Value Return Value Description Iterator<E>An iterator over the elements in reverse order.
Important
The iterator is fail-fast, like all list iterators. Changing the list with list.add or list.remove during the walk causes ConcurrentModificationException. Use the iterator's own remove() instead. The method is declared by Deque, so a variable typed as List cannot call it.
Compare with Similar Methods Ways to walk a list backward:
Java Examples 1. Print in reverse 2. Make a reversed copy 3. Remove from the back Example 1 — Print a List from Last to First Walk a list of lap times backward and number each line.
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDescendingDemo {
public static void main(String[] args) {
LinkedList<String> laps = new LinkedList<>();
laps.add("lap 1: 62s");
laps.add("lap 2: 59s");
laps.add("lap 3: 61s");
Iterator<String> it = laps.descendingIterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("original: " + laps);
}
} lap 3: 61s
lap 2: 59s
lap 1: 62s
original: [lap 1: 62s, lap 2: 59s, lap 3: 61s] How It Works Laps are stored in order. Lap 1 is first and lap 3 is last.The iterator starts at the tail. The first next() returns lap 3.Then it moves toward the head. The loop prints lap 3, lap 2 and then lap 1.The list is untouched. The final line shows the original order, so nothing was reversed in place.Example 2 — Build a Reversed Copy of a List Copy the elements into a new list by walking the first list backward.
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDescendingCopy {
public static void main(String[] args) {
LinkedList<Integer> original = new LinkedList<>(Arrays.asList(1, 2, 3, 4));
LinkedList<Integer> reversed = new LinkedList<>();
Iterator<Integer> it = original.descendingIterator();
while (it.hasNext()) {
reversed.add(it.next());
}
System.out.println("original: " + original);
System.out.println("reversed: " + reversed);
}
} original: [1, 2, 3, 4]
reversed: [4, 3, 2, 1] How It Works An empty target list. The new list reversed starts with no elements.Walk from the back. The iterator returns 4, 3, 2 and then 1.Append each one. add puts each value at the end of the new list, so the new list ends up in reverse order.Two separate lists. The original is unchanged, and the copy reads 4, 3, 2, 1.Example 3 — Remove Elements While Walking Backward Delete every item that is smaller than 3 while walking from the end, using the iterator's remove.
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDescendingRemove {
public static void main(String[] args) {
LinkedList<Integer> levels = new LinkedList<>(Arrays.asList(1, 5, 2, 8, 3, 1));
Iterator<Integer> it = levels.descendingIterator();
while (it.hasNext()) {
int value = it.next();
if (value < 3) {
System.out.println("removing " + value);
it.remove();
}
}
System.out.println("kept: " + levels);
}
} removing 1
removing 2
removing 1
kept: [5, 8, 3] How It Works Six levels. Three of them, 1, 2 and 1, are below 3.The walk goes backward. The last 1 is met first, then 3, then 8, then 2, then 5, then the first 1.it.remove works as usual. The removed values are reported in the order met: 1, 2, 1.The rest stays in place. The list that is left is 5, 8, 3, in the original order.Frequently Asked Questions How do I loop over a LinkedList in reverse order? Use list.descendingIterator() with hasNext() and next(). Or use listIterator(list.size()) with hasPrevious() and previous().
Does descendingIterator() reverse the list? No. It only walks the list from the end. The list keeps its order.
Can I remove elements with a descending iterator? Yes. Call it.remove() after next(), just as with a normal iterator.
Why can I not call descendingIterator() on a List variable? It belongs to the Deque interface, not to List. Declare the variable as LinkedList or Deque.
How can I reverse a LinkedList permanently? Call Collections.reverse(list). It changes the order of the elements in the list itself.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion