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 Cloning A Linked List With Next And Random Pointer- Set 2 We have already discussed 2 different ways to clone a linked list. In this post, one more simple method to clone a linked list is discussed. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. The idea is to use Hashing. Below is algorithm. Traverse the original link 4 min read Java Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20- 4 min read Java Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to 6 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 For Cloning A Linked List With Next And Random Pointer In O(1) Space Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node of the list. Write a program that clones the given list in O(1) space, i.e., without any extra space. Examples: Input : Head of the 4 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 Rotate Doubly linked list by N nodes Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d As 4 min read Like