Saturday, July 12, 2025

Reversing a LinkedList in Java: A Step-by-Step Guide


Whether you're preparing for coding interviews or building scalable backend systems, knowing how to reverse a LinkedList is a must-have skill in any Java developer's toolkit.

In this blog, we'll cover:

  • What a LinkedList is
  • Why reversing it matters
  • How to reverse it using both iterative and recursive approaches
  • Bonus: Reversing a LinkedList using Java's built-in class

๐Ÿง  What is a LinkedList?

A LinkedList is a linear data structure where each element (called a node) contains:

  • Data
  • A reference (or pointer) to the next node

Example:

Head → 1 → 2 → 3 → 4 → null

Reversing the list would result in:

Head → 4 → 3 → 2 → 1 → null

๐Ÿ”ง Custom Singly LinkedList in Java

First, let's define a simple Node class and build our custom LinkedList.

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

๐Ÿ” Iterative Approach to Reverse a LinkedList

Code:

public Node reverseList(Node head) {
    Node prev = null;
    Node current = head;
    Node next = null;

    while (current != null) {
        next = current.next;   // store next
        current.next = prev;   // reverse current node's pointer
        prev = current;        // move pointers one step forward
        current = next;
    }

    return prev; // new head
}

How it Works:

  • Traverse through the list
  • Reverse the link direction for each node
  • Use three pointers (prev, current, next) to safely navigate and modify links

๐Ÿ”„ Recursive Approach to Reverse a LinkedList

Code:

public Node reverseRecursive(Node head) {
    if (head == null || head.next == null) {
        return head;
    }

    Node rest = reverseRecursive(head.next);
    head.next.next = head;
    head.next = null;

    return rest;
}

How it Works:

  • Recursively reverse all nodes after the head
  • Set the next node's next pointer to the current node
  • Set current node’s next to null (to prevent cycles)

✅ Bonus: Reversing Java’s Built-in LinkedList

If you’re using Java’s LinkedList<E> class from the java.util package:

Code:

LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4));
Collections.reverse(list);
System.out.println(list); // Output: [4, 3, 2, 1]

Super simple — but remember, this only reverses the order of the list, not the underlying pointers like in a custom linked structure.


๐Ÿงช Testing the Reversed List

Here's a quick utility function to print a LinkedList:

public void printList(Node head) {
    while (head != null) {
        System.out.print(head.data + " → ");
        head = head.next;
    }
    System.out.println("null");
}

๐Ÿ Final Thoughts

Reversing a LinkedList is more than an academic exercise — it's foundational for mastering data structures and acing technical interviews. Whether you're iterating or recursing, the underlying logic builds your muscle memory in pointer manipulation and algorithm design.

Next Step? Try reversing a doubly linked list or reversing only part of the list (e.g., from position m to n) for an extra challenge!


๐Ÿ’ฌ What’s your preferred method—iterative or recursive? Let us know in the comments!




This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication

No comments:

Post a Comment