LinkedList Reverse in Java
Problem
Collections.reverse() flips the order of any List in place, including a LinkedList, without needing to know how that list stores its elements internally.
Given a LinkedList of elements, reverse their order in place.
Java Program
import java.util.Collections;
import java.util.LinkedList;
public class LinkedListReverse {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
Collections.reverse(list); // reverses the list in place
System.out.println(list);
}
}Output
Core Logic
Collections.reverse() works through the List interface itself, so it reverses a LinkedList the exact same way it would reverse an ArrayList — by swapping paired elements from both ends inward.
- 1
Collections.reverse(list)takes anyListand reverses its element order directly, modifying the list itself rather than returning a new one. - 2Internally, it walks from both ends toward the middle, swapping the element at each matching pair of positions.
- 3Because it's written against the
Listinterface, the exact same call works whetherlistis aLinkedList, anArrayList, or any otherListimplementation. - 4After the call returns,
listitself now holds its elements in reverse order — there's no separate reversed copy to assign.
[A, B, C, D], Collections.reverse() swaps A with D and B with C, leaving [D, C, B, A].Key Point: This mutates the original list in place — if the pre-reversed order is still needed elsewhere, make a copy of the list before calling reverse(), since there's no way to recover the original order afterward.
Why: Collections.reverse() swaps roughly n/2 pairs of elements using ListIterator access from both ends, with no additional collection allocated.
Key Concepts
Approach 2: Java 8
import java.util.LinkedList;
import java.util.List;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class LinkedListReverseStream {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
// descendingIterator() already walks back to front; stream it into a new reversed list
List<String> reversed = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(list.descendingIterator(), 0), false)
.collect(Collectors.toList());
System.out.println(reversed);
}
}
Output
Core Logic
LinkedList's own descendingIterator() already walks the list back to front, so streaming over it builds a new reversed list without touching the original.
- 1
list.descendingIterator()returns anIteratorthat starts at the last element and walks toward the first — a capability specific to LinkedList's doubly-linked structure. - 2
StreamSupport.stream(Spliterators.spliteratorUnknownSize(...), false)adapts that Iterator into a Stream, since Iterator itself has no.stream()method. - 3
.collect(Collectors.toList())gathers the reverse-order elements into a new List. - 4Unlike
Collections.reverse(), the originallistis left completely untouched —reversedis a separate, independent list.
[A, B, C, D], the descending iterator yields D, then C, then B, then A, collecting into [D, C, B, A].Key Point: This is a genuinely different behavior from Collections.reverse(), not just different syntax — the original list stays in its original order, which matters if other code still holds a reference to it; it also leans on a capability specific to LinkedList, not something every List implementation offers directly.
Why: descendingIterator() visits each of the n elements once, and collect() builds a new list to hold all of them, unlike Collections.reverse()'s in-place O(1)-extra-space swaps.