Reverse a Linked List
Reverse a Linked List
Problem Statement
Given a singly linked list, the task is to reverse the list, meaning that the last node becomes the first node, the second-to-last node becomes the second node, and so on.
Table Of Content
- Reverse a Linked List
- Problem Statement
- Recursive Approach
- Implementation of Recursive Approach
- C++ Implementation
- Java Implementation
- Python Implementation
- Iterative Approach
- Dry Run of the Iterative Approach
- Implementation of Iterative Approach
- C++ Implementation
- Java Implementation
- FAQs
- 1.What is a linked list?
- 2.Why would you want to reverse a linked list?
- 3.What is the difference between reversing a linked list iteratively and recursively?
- 4.Is there a preferred approach (iterative vs. recursive) for reversing a linked list?
- 5.What is the time complexity of reversing a linked list?
Recursive Approach
The recursive approach to reversing a linked list involves breaking the problem into smaller subproblems. Here are the general steps:
- Divide the linked list into two parts: the first node (head) and the rest of the list (the linked list without the first node).
- Recursively reverse the rest of the linked list.
- After the recursion, change the next of the last node of the reversed rest to point to the first node.
- Set the next of the first node to NULL to make it the new tail.
Implementation of Recursive Approach
C++ Implementation
#include <iostream>
Java Implementation
Python Implementation
Iterative Approach
The iterative approach to reversing a linked list involves traversing the list once and modifying the next pointers of each node to reverse the direction of the list.
Dry Run of the Iterative Approach
Let’s dry run the iterative approach on a simple example:
Original Linked List: 1 -> 2 -> 3 -> 4 -> null
- Initialize three pointers: prev = null, current = head (1st node), and next = null.
- Iterate through the linked list.
-
- In each iteration, update the next pointer of the current node to point to the previous node (reverse the link).
- Update prev to be the current node.
- Update current to be the next node (move forward).
- After the loop, update the head to be prev, which is now pointing to the new first node (4).
- The reversed linked list is now: 4 -> 3 -> 2 -> 1 -> null.
Implementation of Iterative Approach
C++ Implementation
#include <iostream>
Java Implementation
FAQs
1.What is a linked list?
A linked list is a linear data structure consisting of nodes, where each node points to the next node in the sequence. It’s used to store and manage a collection of elements.
2.Why would you want to reverse a linked list?
Reversing a linked list can be useful in various scenarios, such as when you need to process elements in a different order, search for elements from the end, or improve the efficiency of certain operations.
3.What is the difference between reversing a linked list iteratively and recursively?
The iterative approach reverses the linked list by iteratively modifying the next pointers of each node to change the direction of the list. The recursive approach divides the problem into smaller subproblems, recursively reverses the rest of the list, and then combines the results.
4.Is there a preferred approach (iterative vs. recursive) for reversing a linked list?
The choice between the iterative and recursive approaches often depends on personal preference and the specific requirements of the problem. Both approaches are valid and can achieve the same result.
5.What is the time complexity of reversing a linked list?
Both the iterative and recursive approaches have a time complexity of O(n), where n is the number of nodes in the linked list. This is because they visit each node once.

