LeetCode / Classic Linked List · Pointer Reversal Easy

Linked List — Reverse (Iterative Pointer Flip)

LC
Practice this on LeetCode — "Reverse Linked List" is the foundational pointer-manipulation problem.
Open Problem

Reverse a singly linked list in place by flipping each node's next pointer backward, walking three pointers (prev, curr, next) once. O(N) time, O(1) space

What does it solve?

Turn 1→2→3→4→∅ into 4→3→2→1→∅, reusing the same nodes. No new list, no array copy: just redirect each node's pointer to face backward.

Key insight: walk the list with three references. prev starts null, curr at the head. Each step: stash next = curr.next (so you don't lose the rest), flip curr.next = prev, then slide both forward (prev = curr, curr = next). The order matters: save next before flipping, or you strand the remaining nodes. When curr hits null, prev is the new head.

The core idea

// three-pointer in-place flip
prev = null, curr = head
while curr:
  next = curr.next // save
  curr.next = prev // flip
  prev = curr // advance
  curr = next
return prev

Complexity

Time complexity
O(N)

Each node is visited exactly once and its pointer flipped in constant time. A single pass over the list.

Space complexity
O(1)

Three pointers, regardless of list length. In-place: the recursive version instead costs O(N) stack space.


Lists & edge cases

Live animation — each node's arrow flips to point back at prev
prev (reversed head) curr (flipping) next (saved) pointer flip
Linked list
Pointer state
prev = — curr = — next = — flips = —
Step — of —
prev
—
reversed head
curr
—
being flipped
next
—
saved ahead
flips
—
done
Select a list and press Play to start.
✓ Done
Speed Medium

Step-by-step table

Step Sub-op prev curr next List so far

C++ implementation

reverse_list.cpp
#include <bits/stdc++.h>
using namespace std;

struct ListNode { int val; ListNode* next; };

// Iterative reversal: flip each next pointer, walking prev/curr/next.
ListNode* reverseList(ListNode* head) {
    ListNode* prev = nullptr;
    ListNode* curr = head;

    while (curr) {
        ListNode* next = curr->next;  // save the rest first
        curr->next = prev;             // flip the pointer backward
        prev = curr;                   // advance prev
        curr = next;                   // advance curr
    }
    return prev;                      // prev is the new head
}

My Notes

📝 Notes ✅

▼