Blind 75Easy

Reverse Linked List — Iterative Pointer Reversal

Walk the list once while rewiring next pointers to build the reversed list in-place.

Linked List

Problem Statement

Reverse a singly linked list and return the new head.

Why This Problem Matters

  • Core linked-list manipulation skill that appears in many follow-up problems.
  • Tests pointer handling and understanding of in-place mutations.
  • Sets up more advanced questions like reversing sublists and k-group reversals.

Thought Process

Track three pointers

Use prev, current, and next to safely redirect links without losing the rest of the list.

Iteratively flip next pointers

For each node, store next, point current.next to prev, then advance prev/current.

Return the new head

After loop ends, prev points to the new head while current is null.

Step-by-Step Reasoning

  1. Initialise prev = null and current = head.
  2. While current != null, store next = current.next.
  3. Set current.next = prev, then move prev = current and current = next.
  4. Return prev.

Dry Run

List 1→2→3→4→5

Pointers move one step per iteration; final list 5→4→3→2→1.

Single node list

Loop runs once, prev becomes head, list unchanged logically.

Complexity Analysis

Time

O(n)

Space

O(1)

Why

Traverses the list once with constant extra pointers.

Annotated Solution

JAVA
public class ReverseLinkedList {
  public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode current = head;

    while (current != null) {
      ListNode next = current.next;
      current.next = prev;
      prev = current;
      current = next;
    }

    return prev;
  }

  public static class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
  }
}

Reversing links iteratively is safe because the next pointer is saved before mutation, ensuring no nodes are lost.

Common Pitfalls

  • Forgetting to store next before reassigning current.next loses access to the remainder.
  • Returning current instead of prev leaves null head.
  • Recursive solutions risk stack overflow on long lists—mention iterative approach advantages.

Follow-Up Questions

  • How would you reverse a sublist between positions left and right?
  • Can you reverse nodes in groups of k?

Key Takeaways

Pointer discipline is essential in linked-list questions.
Iterative reversal is preferable for constant space and stack safety.
Memorise the three-pointer pattern—it reappears often.