πDay 1. Reverse a linked list π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You are given the head of a singly linked list. Your task is to reverse the list and return the reversed head.
π Example Walkthrough:
Input:
head: 1 -> 2 -> 3 -> 4 -> NULL
Output:
head: 4 -> 3 -> 2 -> 1 -> NULL
Explanation:
Input:
head: 2 -> 7 -> 10 -> 9 -> 8 -> NULL
Output:
head: 8 -> 9 -> 10 -> 7 -> 2 -> NULL
Explanation:
Input:
head: 10 -> NULL
Output:
head: 10 -> NULL
Explanation:
Constraints:
1 <= number of nodes, data of nodes <= $10^5$
π― My Approach:
Iterative Reversal Algorithm: The problem can be efficiently solved using an iterative approach by traversing the linked list and reversing the
nextpointers of each node.Start with two pointers:
prev(initialized toNULL) andcurrent(initialized to the head of the list).In each iteration, update the
nextpointer of thecurrentnode to point to theprevnode, then moveprevandcurrentforward.Continue the process until the entire list is reversed.
Steps:
Initialize
prevtoNULLandcurrentto the head of the list.Traverse the list while
currentis notNULL.For each node, reverse the
nextpointer to point toprev.Move the
prevandcurrentpointers one step forward.Once the list is completely reversed, return
prevas the new head.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where
nis 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
prevandcurrentpointers).
π Solution Code
Code (C++)
π¨βπ» Alternative Approach
Pointer Manipulation Using swap for Linked List Reversal
swap for Linked List ReversalExplanation:
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:
previs initialized tonullptr(to mark the new end of the reversed list).headstarts 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->nextwithprev: This updates the current node'snextpointer to point to the previous node.Swap
headwithprev: This shifts theprevpointer to the current node (marking progress in the reversed list) and moves theheadpointer forward to the next node in the original list.
Termination:
The loop continues until
headbecomesnullptr(indicating the end of the list has been reached).At this point,
prevholds the new head of the reversed linked list.
Return Value:
The method returns
prev, which now points to the reversed list's head.
Solution
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: $(1 \rightarrow 2 \rightarrow 3 \rightarrow \text{nullptr})$
Initialization:
prev = nullptr,head = 1First Iteration:
Swap
head->nextandprev: Now1->next = nullptrSwap
headandprev:prev = 1,head = 2
Second Iteration:
Swap
head->nextandprev: Now2->next = 1Swap
headandprev:prev = 2,head = 3
Third Iteration:
Swap
head->nextandprev: Now3->next = 2Swap
headandprev:prev = 3,head = nullptr
Termination:
headisnullptr, andprevpoints to the reversed list: $(3 \rightarrow 2 \rightarrow 1 \rightarrow \text{nullptr})$
Code (Java)
Code (Python)
π― Contribution and Support:
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! β
πVisitor Count
Last updated