How to reverse a singly linked list using only two pointers?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reversing a singly linked list is a classic algorithmic problem often encountered in technical interviews and computer science courses. A singly linked list is a data structure made of nodes where each node consists of data and a reference (link) to the next node in the list. Reversing such a list involves changing the direction of pointers so that the last node becomes the head and the head becomes the last node. Typically, three pointers are used to achieve this, but it is possible to reverse a singly linked list using only two pointers. In this article, we explore how to accomplish this and dive into the technical details.
Understanding Singly Linked Lists
Before diving into the reversal process, it is essential to understand the basic structure of a singly linked list:
- Node: A basic unit of a linked list consisting of data and a reference to the next node.
- Head: The first node in the linked list.
- Tail: The last node in the list, which points to
NULL.
Example of a Singly Linked List
Reversing the Linked List Using Two Pointers
In the conventional method of reversing a linked list, three pointers—previous, current, and next—are typically used. Here, we focus on a more elegant technique that uses only two pointers.
Algorithm Explanation
- Initialize Pointers: Begin by initializing two pointers:
prevasNULL(since the new tail’snextshould beNULL).currentpointing to the head of the list.
- Iterate through the List: Use a loop to traverse through the list until the
currentpointer becomesNULL. - Reversal Process:
- In each iteration, memorize the next node by storing
current->nextin a temporary variabletemp. - Reverse the
nextreference of thecurrentnode to point toprev. - Move the
prevandcurrentpointers one step forward (prevbecomescurrentandcurrentbecomestemp).
- Update the Head: Once
currentisNULL, theprevpointer will be positioned at the new head of the reversed list.
Detailed Code Example
Here is a C-style pseudo-code illustrating the two-pointer method:
Key Pointers and Steps
The table below summarizes the key pointers and steps in the reversal process:
| Step | prev Pointer | current Pointer | Action Taken |
| 1 | NULL | head | Initialize pointers |
| 2 | NULL | current | Memorize current->next |
| 3 | current | temp | Reverse current->next |
| 4 | prev | current | Update pointers (prev, current) |
| 5 | Continue Until current is NULL | List is now reversed |
Complexity Analysis
Time Complexity
The time complexity of this algorithm is , where is the number of nodes in the linked list. This is due to the single pass through the list in the while loop.
Space Complexity
The space complexity is because we only use a constant amount of extra space, merely two pointers and a temporary variable.
Conclusion
Reversing a singly linked list is a fundamental but crucial operation, and understanding how to do so with only two pointers provides elegant insight into the mechanics of pointer manipulation. This approach minimizes auxiliary storage while maintaining simplicity and efficiency. Mastering these fundamental concepts is invaluable for anyone interested in algorithms or data structures.
By focusing exclusively on two pointers, this method captures the essence of in-place algorithm design and emphasizes the power of thoughtful data manipulation in reducing complexity.

