Data Structures19 sections · 729 units
Open in Course

Iterative Reversal

Three-pointer technique

The iterative approach uses three pointers: prev, current, and next.

prev = null
current = head
while current != null:
    next = current.next
    current.next = prev
    prev = current
    current = next
return prev

Walk through this carefully. At each step:

1.1. Save current.next before overwriting it

2.2. Point current.next backward to prev

3.3. Advance prev and current one step

When current becomes null, prev points to the old tail, which is now the new head.

Time: O(n)O(n). Space: O(1)O(1).