How to reverse a LinkedList in Java

Hello people! today we are going to learn the concept of reversing a LinkedList in Java. Before we move to learn how to reverse a LinkedList in Java, let me tell you that LinkedList is a data structure where every data element is in the form of a node and each node contains the address of the next node. This sets a link between both the nodes. There are various situations where the reverse of a LinkedList is required such as to check whether a LinkedList is a palindrome or not. There are several ways to reverse a LinkedList. Today we are going to learn one of them.

How to reverse a LinkedList in Java

Let’s look at the solution, here we will traverse every node and change the direction of the link of every node which is pointing to a forward node. we will make it point to the previous node. In this way, the first node will point to null and the last node will become the head of the LinkedList.

Original LinkedList:-                                                          After Reversing LinkedList:- 

head  ->4  ->5  ->8  ->9  ->null                                                null<- 4 <- 5 <- 8 <- 9 <- head

Here is the code for the same:-

import java.util.Scanner;
class Node {               
  int data;
  node next;

  node(int d) {
    data = d;
    next = null;

public class reverse {
  Node head;

  void printlist(Node head)                       // function to print the LinkedList
    Node temp = head;
    System.out.print("Head-> ");
    while (temp != null) {
      System.out.print( + "-> ");
      temp =;

  public void addToTheLast(Node node)     // add new node at the end of the LinkedList
    if (head == null)
      head = node;
    else {
      Node temp = head;
      while ( != null) {
        temp =;
      } = node;

  public static void main(String[] args)               //Main function
    Scanner sc = new Scanner(;

    reverseClass llist2 = new reverseClass();          //object of class reverseClass
    System.out.println("Enter the size of linklist");
    int n = sc.nextInt();
    reverse llist1 = new reverse();                     //object of class reverse
    System.out.println("Enter the integer data in linklist");
    int a1 = sc.nextInt();
    Node head = new Node(a1);
    llist1.addToTheLast(head);                        // add head to linkedlist

    for (int i = 1; i < n; i++) {                      //add remaining nodes
      int a = sc.nextInt();
      llist1.addToTheLast(new Node(a));
    System.out.println("Before reversing"); 

    Node head2 = null;
    System.out.println("After reversing");
    head2 = llist2.reverse(head);


class reverseClass {
  Node reverse(Node head)                // reverse function 

    Node temp = head, prev = null, n =;
    while (n != null) { = prev;                // changing link of node 
      prev = temp;
      temp = n;
      n =;

    } = prev;
    head = temp;

    return head;



Enter the size of linklist
Enter the integer data in linklist
Before reversing
Head-> 6-> 2-> 1-> 4-> 8-> NULL
After reversing
Head-> 8-> 4-> 1-> 2-> 6-> NULL

The time complexity of the code:- O(n)

Because we are traversing each node only one time. we can also reverse a LinkedList without actually changing the links of the node. We can do so by using another data structure called Stack.

Hope you understand the code.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *