Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to reverse a linked list in Java

In this tutorial, we will see how to reverse a linked list in java. LinkedList is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. But before moving further, if you are not familiar with the concept of the linked list in java, then do check the article on Linked List in Java.

Input: Enter the Linked List elements: 6 7 8 4 5

Output: Before Reversing the Linked List: 6 7 8 9 5

After Reversing the Linked List: 5 9 8 7 6

This can be done by using the following methods:

Approach 1: Using an In-built Method

Approach 2: Without Using an In-built Method

Let us look at each of these approaches for a better understanding.

Program 1: Java Program to Reverse a Linked List

In this program, we will see how to reverse a linked list in java using the collections class.

Algorithm:

  1. Start
  2. Declare a linked list of integer types without any initial size.
  3. Use the add method to add the elements.
  4. Append the elements at the end of the list.
  5. Print the linked list elements before reversing.
  6. Use the In-built Collections.reverse() method for reversing a linked list.
  7. Print the linked list elements after reversing.
  8. Stop

Let us look at the below example for a better understanding of the above algorithm.

// Java program for reversing a linked list using in-built collections class
import java.util.*;
  
public class Main 
{
    public static void main(String[] args)
    {
        // Declaring linkedlist without any initial size
        LinkedList<Integer> ll = new LinkedList<Integer>();
  
        // Appending elements at the end of the list
        ll.add(new Integer(1));
        ll.add(new Integer(2));
        ll.add(new Integer(3));
        ll.add(new Integer(4));
        ll.add(new Integer(5));
        System.out.println("The elements of the linked list before reversing: " + ll);
        // Collections.reverse method to reverse the list
        Collections.reverse(ll);
        System.out.println("The elements of the linked list after reversing: " + ll);
    }
}


The elements of the linked list before reversing: [1, 2, 3, 4, 5]
The elements of the linked list after reversing: [5, 4, 3, 2, 1]

Program 2: Java Program to Reverse a Linked List

In this program, we will see how to reverse a linked list in java using any in-built methods.

Algorithm:

  1. Start
  2. Declare a linked list of string types without any initial size.
  3. Use the add method to add the elements.
  4. Append the elements at the end of the list.
  5. Print the linked list elements before reversing.
  6. Use a user-defined function for reversing.
  7. Take the linked list as a parameter and return the reverse linked list.
  8. Run the loop for n/2 times where ‘n’ is the number of elements in the linked list.
  9. In the first pass, Swap the first and nth element.
  10. In the second pass, Swap the second and (n-1)th element and so on till you reach the mid of the linked list.
  11. Return the linked list after loop termination.
  12. Print the linked list elements after reversing.
  13. Stop

Let us look at the below example for a better understanding of the above algorithm.

// Java program to reverse a linked list
import java.util.*;

public class Main 
{
	public static void main(String[] args)
	{
		LinkedList<String> ll = new LinkedList<String>();
		ll.add(new String("Physics"));
		ll.add(new String("Maths"));
		ll.add(new String("Java"));
		ll.add(new String("English"));
		ll.add(new String("Chemistry"));
		System.out.println("The elements of the linked list before reversing: " + ll);
		// Calling user defined function for reversing
		ll = reverseLinkedList(ll);
		System.out.println("The elements of the linked list after reversing: " + ll);
	}
	//Takes a linkedlist as a parameter and returns a reversed linked list
	public static LinkedList<String> reverseLinkedList(LinkedList<String> ll)
	{
		for (int i = 0; i < ll.size() / 2; i++) 
		{
			String temp = ll.get(i);
			ll.set(i, ll.get(ll.size() - i - 1));
			ll.set(ll.size() - i - 1, temp);
		}
		// Return the reversed arraylist
		return ll;
	}
}


The elements of the linked list before reversing: [Physics, Maths, Java, English, Chemistry]
The elements of the linked list after reversing: [Chemistry, English, Java, Maths, Physics]