Java Program to Reverse a Linked List Without Manipulating its Pointers Last Updated : 22 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Given a linked list, the task is to write a program in Java that reverses the linked list without manipulating its pointers, i.e., the reversal should happen just by changing the data values and not the links. ExamplesInput: Original linked list 1->2->3->4->5->nullOutput: Linked list after reversal5->4->3->2->1->null Input: Original linked list5->14->20->8->nullOutput: Linked list after reversal8->20->14->5->null Input: Original linked list80->nullOutput: Linked list after reversal80->null For reversal, no manipulation is done in the links that connect the node of the linked list. Only the data values are changed. To understand this more clearly, take a look at the following diagram. The numbers in red color represent the addresses of nodes. It is to be noticed that even after the reversal, the links remained the same, i.e., before reversing, the node at address 100 was connected to the node at address 200, which in turn was connected to the node at address 300 and so on, and these connections have remained the same after the reversal as well. Approach:Variables 'l' and 'r' are initialized as 0 and size-1 respectively, denoting the index of first and last node.In a loop, the nodes at index 'l' and 'r' are obtained and the corresponding data values are swapped. The loop works by incrementing 'l' and decrementing 'r'.For getting the node at a particular index, a private method 'fetchNode' is defined.Program to Reverse a Linked List Without Manipulating its PointersBelow is the implementation of the above approach: Java // Java program to reverse a linked list without pointer // manipulation class Node { int value; Node next; Node(int val) { value = val; next = null; } } public class LinkedList { Node head; // this function returns the Node which is at a // particular index. // (The index is passed as the argument) private Node fetchNode(int index) { Node temp = head; for (int i = 0; i < index; i++) { temp = temp.next; } return temp; } // this function returns the size of linked list int getSize(Node head) { Node temp = head; int size = 0; while (temp != null) { size++; temp = temp.next; } return size; } // function to reverse the linked list void reverse() { int l = 0; int r = getSize(this.head) - 1; while (l < r) { Node leftSideNode = fetchNode(l); Node rightSideNode = fetchNode(r); int t = leftSideNode.value; leftSideNode.value = rightSideNode.value; rightSideNode.value = t; l++; r--; } } // function that prints the elements of linked list void printLinkedList() { Node temp = this.head; while (temp != null) { System.out.print(temp.value + " "); temp = temp.next; } System.out.println(); } // Driver code public static void main(String[] args) { LinkedList list1 = new LinkedList(); list1.head = new Node(1); list1.head.next = new Node(2); list1.head.next.next = new Node(3); list1.head.next.next.next = new Node(4); list1.head.next.next.next.next = new Node(5); System.out.println("Linked List Before Reversal: "); list1.printLinkedList(); list1.reverse(); System.out.println("Linked List After Reversal: "); list1.printLinkedList(); } } Complexity of the above methodTime complexity: O(n2) where n is no of nodes in linked list. As there is a nested search for l and r. Hence, O (n*n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Reverse a Linked List Without Manipulating its Pointers B bhatiahansika99 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 java-LinkedList +1 More Practice Tags : Java Similar Reads Java Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In 3 min read Java Program To Flatten A Multi-Level Linked List Depth Wise- Set 2 We have discussed flattening of a multi-level linked list where nodes have two pointers down and next. In the previous post, we flattened the linked list level-wise. How to flatten a linked list when we always need to process the down pointer before next at every node. Input: 1 - 2 - 3 - 4 | 7 - 8 - 4 min read Java Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 6 min read Java Program For Pointing Arbit Pointer To Greatest Value Right Side Node In A Linked List Given singly linked list with every node having an additional âarbitraryâ pointer that currently points to NULL. We need to make the âarbitraryâ pointer to the greatest value node in a linked list on its right side. A Simple Solution is to traverse all nodes one by one. For every node, find the node 5 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Java Program For Reversing A Linked List In Groups Of Given Size - Set 1 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL Recommended: Please solve 3 min read Java Program For Reversing Alternate K Nodes In A Singly Linked List Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm. Example: Inputs: 1->2->3->4->5->6->7->8->9->NULL and k = 3 Output: 3->2->1->4->5->6- 6 min read Java Program For Reversing A Linked List In Groups Of Given Size- Set 2 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->N 3 min read Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 5 min read Like