206. Reverse Linked ListΒΆ
To solve this in a single pass, we can use two pointers, prev and curr.
The prev pointer represents the previous node and will become the next pointer for the current node curr.
While reversing the link for curr, we must first store its original next node in a temporary variable.
This prevents losing the remaining part of the list and allows us to move forward to the next node.
Runtime Complexity
Time: \(O(n)\)
Space: \(O(1)\)