Java ProgramsCollectionsLinkedList Reverse

LinkedList Reverse in Java

beginner·  Collections  ·  List

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.

Input
[A, B, C, D]
Output
[D, C, B, A]

Java Program

Java
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

[D, C, B, A]

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.

How It Works
  1. 1Collections.reverse(list) takes any List and reverses its element order directly, modifying the list itself rather than returning a new one.
  2. 2Internally, it walks from both ends toward the middle, swapping the element at each matching pair of positions.
  3. 3Because it's written against the List interface, the exact same call works whether list is a LinkedList, an ArrayList, or any other List implementation.
  4. 4After the call returns, list itself now holds its elements in reverse order — there's no separate reversed copy to assign.
For [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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Collections.reverse() swaps roughly n/2 pairs of elements using ListIterator access from both ends, with no additional collection allocated.

Key Concepts

LinkedListCollections.reverse()in-place mutation

Approach 2: Java 8

Java
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

[D, C, B, A]

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.

How It Works
  1. 1list.descendingIterator() returns an Iterator that starts at the last element and walks toward the first — a capability specific to LinkedList's doubly-linked structure.
  2. 2StreamSupport.stream(Spliterators.spliteratorUnknownSize(...), false) adapts that Iterator into a Stream, since Iterator itself has no .stream() method.
  3. 3.collect(Collectors.toList()) gathers the reverse-order elements into a new List.
  4. 4Unlike Collections.reverse(), the original list is left completely untouched — reversed is a separate, independent list.
For [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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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.

Key Concepts

descendingIterator()StreamCollectors.toList()

Related Programs