How to Reverse Linked List in JavaIn this section, we will discuss how to reverse a linked list in Java. Reversing a linked list is one of the most prominent topics that is asked in interviews. The task is to reverse a linked list, provided the head or the first node of the linked list is given. Given: Head or the first node of the Linked List (which is 4 in our case) 4 -> 6 -> 7 -> 1 -> 5 - > 8 -> 3 -> 2 -> NULL Return: 2 -> 3 -> 8 -> 5 -> 1 -> 7 -> 6 -> 4 -> NULL Given: 3 -> NULL Return: 3 -> NULL Given: NULL Return: NULL There are two approaches to solve the problem. These two approaches are:
Let's discuss the iterative approach first. Reversing a Linked List Using Iterative ApproachThe following are some steps involved in the iterative approach. Step 1: Take three-pointers (previous, current, and next), and initialize them as the following. previous = NULL, current = head, next = NULL. Step 2: Iterating through the linked list using a loop, and do following. ImplementationThe following code shows the implementation of the reversal of the linked list with the help of steps defined above. FileName: LinkedListIterative.java 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 Time & Space Complexity: The time complexity of the above program is O(n), whereas the space complexity of the program is O(1), where n represents the total number of nodes present in the list. Reverse a LinkedList Using Recursive ApproachThe following are some steps involved in the recursive approach. Step 1: Split the list given into two parts - the first node and the rest of the linked list. Step 2: Invoke the reverseList() method for the remaining portion of the linked list. Step 3: Join the rest to the first. Step 4: Fix the head pointer. ImplementationThe following code shows the implementation of the reversal of the linked list with the help of the steps defined above. FileName: LinkedListRecursive.java 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 Time & Space Complexity: The time complexity of the above program is O(n), whereas the space complexity of the program is O(1), where n represents the total number of nodes present in the list. Note that the above program uses the inbuilt stack because of the recursion. For the sake of simplicity, we have ignored the space consumed by the inbuilt stack. Reversing a Linked List Using StackThe following are the steps used when one does the reversal of the linked list using stack. Step 1: Keep the values of the nodes in the stack until all the values of the nodes are entered. Step 2: Update the Head pointer using the value of the last node of the list. Step 3: Keep removing the values of the nodes from the stack and start appending them to the head node until the stack is empty. Step 4: Ensure that after appending work is done, the last node of the list is pointing to NULL. ImplementationThe following code shows the implementation of the reversal of the linked list with the help of the steps defined above. FileName: LinkedListStack.java 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 Time & Space Complexity: The time complexity of the above program is O(n), whereas the space complexity of the program is also O(n), where n represents the total number of nodes present in the list. Reversing a Linked List Using an ArrayThe following are the steps used when one does the reversal of the linked list using an array. Step 1: Count the number of nodes present in the given list. Step 2: Create an integer array such that the size of the array is equal to the size of the list. Step 3: Iterate over the list and fill the array using the value of nodes from left to right. Step 4: From the end of the array, take the array elements one by one and create a list from it such that the last element of the array forms the head of the list. The second last element of the array forms the second node of the list, and so on. ImplementationThe following code shows the implementation of the reversal of the linked list with the help of the steps defined above. FileName: LinkedListArray.java 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 Time & Space Complexity: The time complexity of the above program is O(n), whereas the space complexity of the program is also O(n), where n represents the total number of nodes present in the list. |