The problem can be found at the following link: Problem Link
You are given the head of a singly linked list. Your task is to reverse the list and return the reversed head.
Input:
head: 1 -> 2 -> 3 -> 4 -> NULL
Output:
head: 4 -> 3 -> 2 -> 1 -> NULL
Input:
head: 2 -> 7 -> 10 -> 9 -> 8 -> NULL
Output:
head: 8 -> 9 -> 10 -> 7 -> 2 -> NULL
Input:
head: 10 -> NULL
Output:
head: 10 -> NULL
- 1 <= number of nodes, data of nodes <=
$10^5$
-
Iterative Reversal Algorithm:
The problem can be efficiently solved using an iterative approach by traversing the linked list and reversing thenext
pointers of each node.- Start with two pointers:
prev
(initialized toNULL
) andcurrent
(initialized to the head of the list). - In each iteration, update the
next
pointer of thecurrent
node to point to theprev
node, then moveprev
andcurrent
forward. - Continue the process until the entire list is reversed.
- Start with two pointers:
-
Steps:
- Initialize
prev
toNULL
andcurrent
to the head of the list. - Traverse the list while
current
is notNULL
. - For each node, reverse the
next
pointer to point toprev
. - Move the
prev
andcurrent
pointers one step forward. - Once the list is completely reversed, return
prev
as the new head.
- Initialize
- Expected Time Complexity: O(n), where
n
is the number of nodes in the linked list. We traverse the entire list once. - Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space (for the
prev
andcurrent
pointers).
class Solution {
public:
Node* reverseList(Node* head) {
Node *prev = NULL, *curr = head, *next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
👨💻 Alternative Approach
Pointer Manipulation Using swap
for Linked List Reversal
Explanation:
This approach leverages pointer manipulation to reverse the linked list in place, using the swap
function to reduce the verbosity of code and streamline pointer updates. The key idea is to iteratively reverse the next
pointers of each node while traversing the list.
Detailed Steps:
-
Initialization:
-
prev
is initialized tonullptr
(to mark the new end of the reversed list). -
head
starts as the current node in the original list.
-
-
Iterative Reversal:
- In each iteration of the loop, two swaps are used to reassign pointers:
- Swap
head->next
withprev
: This updates the current node'snext
pointer to point to the previous node. - Swap
head
withprev
: This shifts theprev
pointer to the current node (marking progress in the reversed list) and moves thehead
pointer forward to the next node in the original list.
- Swap
- In each iteration of the loop, two swaps are used to reassign pointers:
-
Termination:
- The loop continues until
head
becomesnullptr
(indicating the end of the list has been reached). - At this point,
prev
holds the new head of the reversed linked list.
- The loop continues until
-
Return Value:
- The method returns
prev
, which now points to the reversed list's head.
- The method returns
Solution
class Solution {
public:
Node* reverseList(Node* head) {
Node *prev = nullptr;
while (head) {
swap(head->next, prev), swap(head, prev);
}
return prev;
}
};
Key Characteristics:
-
Runtime Efficiency:
- The approach has a time complexity of (O(n)), where (n) is the number of nodes in the list. Each node is processed exactly once.
-
Space Efficiency:
- The space complexity is (O(1)) as no additional memory is used apart from a few pointers.
Example Walkthrough:
Let the initial linked list be:
-
Initialization:
prev = nullptr
,head = 1
-
First Iteration:
- Swap
head->next
andprev
: Now1->next = nullptr
- Swap
head
andprev
:prev = 1
,head = 2
- Swap
-
Second Iteration:
- Swap
head->next
andprev
: Now2->next = 1
- Swap
head
andprev
:prev = 2
,head = 3
- Swap
-
Third Iteration:
- Swap
head->next
andprev
: Now3->next = 2
- Swap
head
andprev
:prev = 3
,head = nullptr
- Swap
-
Termination:
-
head
isnullptr
, andprev
points to the reversed list:$(3 \rightarrow 2 \rightarrow 1 \rightarrow \text{nullptr})$
-
class Solution {
Node reverseList(Node head) {
Node prev = null;
while (head != null) {
Node next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
class Solution:
def reverseList(self, head):
prev = None
while head:
next = head.next
head.next = prev
prev = head
head = next
return prev
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐