
REVERSING THE LINKEDLIST
How I Understood Reversing a Linked List (LeetCode 206) When I first saw this problem, reversing a linked list seemed tricky because we can’t access nodes by index like arrays. After breaking it down step by step, it became clear that we can do it iteratively with just three pointers. ** Problem** Given the head of a singly linked list, reverse the list and return its new head. Example: Python Input: 1 -> 2 -> 3 -> 4 -> 5 -> None Output: 5 -> 4 -> 3 -> 2 -> 1 -> None What I Noticed The key is to reverse the next pointers for each node. To avoid losing the rest of the list, we need to store the next node before changing the pointer. What Helped Me Using three pointers made it very clear: prev → initially None curr → initially head next_node → temporarily store curr.next Step-by-step process for each node: Save the next node (next_node = curr.next) Reverse the current pointer (curr.next = prev) Move forward (prev = curr, curr = next_node) Repeat until curr becomes None. At the end, prev
Continue reading on Dev.to Beginners
Opens in a new tab



