Java Program To Reverse A Linked List Without Manipulating Its Pointers


Linked List and Pointers In Java

Linked list is liner data structure in Java environment, which use to store the elements in an adjacent manner. A linked list contains with some addresses and pointers which are used to create a link between the elements present in a list. There are two types of elements present in a linked list. Those are - data element and address element. The value of an element indicated by the data part where the address part helps to create a link between the elements aka node.

In Java there is an another concept of Pointers. A pointer is an address of some specific locations in memory. They hold an important role as a reference to the objects. In this article we will learn how to Reverse a Linked List Without Manipulating its Pointers using Java methodologies.

  • In the reversal operation of a linked list, no manipulation will be done with the links. It connects the nodes of the linked list with the previous one.

  • For linked list there are three pointers - current, previous, and next. These are used to reach the last node of the linked list with the recursion method.

  • Process to achieve the outcome −

    • List division -first node and linked list.

    • Call reverse

    • Link first node with the rest.

    • Head pointer is NULL.

Algorithm Of Reversed Linked List

Here is the general algorithm for a reversed linked lint by using Java −

  • Step 1 − Class initialization.

  • Step 2 − Declare both head and tail as null.

  • Step 3 − Assign a function to find the size of a linked list.

  • Step 4 − Check linked list is empty or not.

  • Step 5 − Print the data.

  • Step 6 − Declare temp = temp.next.

  • Step 7 − Print(end)

  • Step 8 − Add a node at the beginning.

  • Step 9 − Declare a temp node towards head.

  • Step 10 − Find the linked list empty or not.

  • Step 11 − If not, then set the head which points temp node.

  • Step 12 − Add a node at any index.

  • Step 13 − Throw a new exception.

  • Step 14 − Return Temp.

  • Step 15 − int left =0, int right = this.size.

  • Step 16 − Swap the data of left and right node.

  • Step 17 − Data reversed.

Syntax

Followed by Reversed Linked List.

While (current!=NULL){
   next= current->next;
   current->next=prev;
   prev=current;
}
*head_ref=prev;

To reverse a linked list, there are two most crutial steps to follow;

  • Declare the NULL pointer, head and next pointers (where prev and next both are NULL).

  • Iterate a loop through Linked List.

Below approaches are useful for reversing a Linked List without the pointers −

  • Approach 1   Reversal by changing data value

  • Approach 2   Iterative Approach To Reverse A Linked List

Reversal By Changing Data Value

Without manipulation any pointers there is a process to reverse a linked list. It is by changing the value of the data. It means, enter new data in a node to save and use them for futher operation.

Example

class LinkedList{
   Node head;
   class Node{
      int data;
      Node next;
      Node (int x){
         data = x;
         next = null;
      }
   }
   public void display (){
      Node temp = head;

      while (temp != null){
         System.out.print (temp.data + " ");
         temp = temp.next;
      }
      System.out.println ("END");
   }
   public Node insertBeginning (int data){
      Node newNode = new Node (data);
      newNode.next = head;
      head = newNode;
      return head;
   }
   public void reverse (){
      Node pointer = head;
      Node previous = null, current = null;
      while (pointer != null){
         current = pointer;
         pointer = pointer.next;
         current.next = previous;
         previous = current;
         head = current;
      }

   }

}
public class Main{
   public static void main (String[]args){
      try{
         LinkedList ll0 = new LinkedList ();
         ll0.insertBeginning (2);
         ll0.insertBeginning (4);
         ll0.insertBeginning (6);
         ll0.insertBeginning (8);
         System.out.println("LinkedList before reversal : ");
         ll0.display ();
         System.out.println("LinkedList after reversal : ");
         ll0.reverse ();
         ll0.display ();
      }
      catch (Exception e){
         System.out.println ("Exception occurred.");
      }
   }
}

Output

LinkedList before reversal : 
8 6 4 2 END
LinkedList after reversal : 
2 4 6 8 END

Iterative Approach To Reverse A Linked List

Iterative approach is another well known method to reverse a linked list. This method is used in below program.

Example

public class LinkedListIterative {    
   static LinkedListNode head;  
   static class LinkedListNode {  
      int val;   
      LinkedListNode next;   
      LinkedListNode(int no){  
         val = no;  
         next = null;  
      }  
   }  
   LinkedListNode reverse(LinkedListNode node){   
      LinkedListNode previous = null;  
      LinkedListNode curr = node;  
      LinkedListNode next = null;   
      while (curr != null){  
         next = curr.next;  
         curr.next = previous;  
         previous = curr;  
         curr = next;  
      }  
      node = previous;  
      return node;  
   }  
   void printList(LinkedListNode nde){  
      while (nde != null){  
         System.out.print(nde.val + " ");  
         nde = nde.next;  
      }  
   }  
   public static void main(String argvs[]){  
      LinkedListIterative listObj = new LinkedListIterative();  
      listObj.head = new LinkedListNode(4);  
      listObj.head.next = new LinkedListNode(6); 
      listObj.head.next.next = new LinkedListNode(7);  
      listObj.head.next.next.next = new LinkedListNode(1);  
      listObj.head.next.next.next.next = new LinkedListNode(5);  
      listObj.head.next.next.next.next.next = new LinkedListNode(8);  
      listObj.head.next.next.next.next.next.next = new LinkedListNode(3);  
      listObj.head.next.next.next.next.next.next.next = new LinkedListNode(2);  
      System.out.println("The Linked list before reversal is: ");  
      listObj.printList(head);  
      head = listObj.reverse(head);  
      System.out.println("\n");  
      System.out.println("After reversal, the linked list is: ");  
      listObj.printList(head);  
   }  
}  

Output

The Linked list before reversal is: 
4 6 7 1 5 8 3 2 

After reversal, the linked list is: 
2 3 8 5 1 7 6 4

Conclusion

From the above discussion, we have learnt how to change the data of a linked list. As well as in the same manner we build a Java code working with the two pointers named left and right in a list.

Thereby it has been seen how the written code and algorithms help you to get a enlarged view of the mentioned methods.

Updated on: 31-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements