Back to articles
Reverse a Linked List – CA22

Reverse a Linked List – CA22

via Dev.toMohith

My Thinking and Approach Introduction In this problem, I was given the head of a singly linked list and asked to reverse it. At first, I thought it might be tricky because linked lists don’t allow direct indexing like arrays. But once I understood how pointers work, the solution became clear. Problem Statement Given the head of a singly linked list Reverse the list Return the new head My Initial Thought Initially, I thought: Store elements in an array Reverse the array Rebuild the linked list But this uses extra space and is not efficient. So I tried to solve it directly using pointers. Optimized Approach (Iterative) I realized that: Each node points to the next node To reverse, I need to change the direction of these pointers My Approach Use three pointers: prev → previous node curr → current node next → next node Steps Initialize: prev = null curr = head Traverse the list: Store next node → next = curr.next Reverse pointer → curr.next = prev Move prev forward Move curr forward At the

Continue reading on Dev.to

Opens in a new tab

Read Full Article
7 views

Related Articles