Reverse a Linked List

Problem Statement
The objective is to reverse a given linked list, which involves altering the pointers between nodes such that the last node becomes the new head node, and each subsequent node points to its predecessor.
For Example:
Input1 :
Output1 :
Input2 :
Output2 :
Input3 :
Output3 :
Approach 1 : Iterative Method
First, that is the iterative approach to reverse a linked list is given below.
Algorithm
- Initialize three-pointers that are curr, prev, and next with NULL values.
- Go through the linked list iteratively.
Perform the following in a loop :
Code Implementation
- In C++
- In Java
- In Python
Output :
Complexity Analysis
Time Complexity :
As we will iterate over the whole linked list of size "n" so the time complexity of this approach will be .
Space Complexity :
As no extra memory is being used in this approach so auxiliary space consumed will be .
Approach 2 : Recursive Method
Second, that is a recursive approach to reverse a linked list is given below.
Algorithm
- The first node and the remainder of the linked list should be separated into two parts.
- For the remaining linked list items, call reverse.
- Link the rest to the first.
- Fix the head pointer.
Code Implementation
- In C++
- In Java
- In Python
Output :
Complexity Analysis
Time Complexity :
As we are iterating over the linked list with "n" nodes to reverse it then time complexity will be .
Space Complexity :
As we are using extra "n" size space for performing this approach thus it will consume auxiliary space.
Approach 3 : Tail Recursive Method
As in the previous two approaches we have used the iterative and recursive methods now here we will use this tail recursive method to reverse a linked list.
Code Implementation
- In C++
- In Java
- In Python
Output :
Complexity Analysis
Time Complexity :
As we are iterating over the linked list of size "n" for reversing it, so time complexity will be .
Space Complexity :
As no extra space is required in this approach so it will take auxiliary space.
Approach 4: Using Stack
Algorithm
- Store the values in the stack until all the values are entered.
- Update a Head pointer to a final location(last node) once all entries have been made.
- Until the stack is empty, begin popping up the nodes and storing them in the same order.
- Update the last Node’s next pointer in the stack with NULL.
Code Implementation
- In C++
- In Java
- In Python
Output :
Complexity Analysis
Time Complexity :
As we are iterating over the linked list of size "n" for reversing it so time complexity will be .
Space Complexity :
As we are creating a stack and storing "n" nodes values in it. Here "n" is the size of the linked list. So auxiliary space is used here.
Conclusion
-
We have seen five approaches to reverse a linked list.
These are :
- Iterative Method,
- Recursive Method,
- Tail Recursive Method,
- Using Array,
- Using Stack.
-
An iterative method is the best one for reversing the linked list under time complexity and space complexity.
-
Using the stack approach takes time and space for reversing the linked list.