LazyCoder

Reverse a Linked List

Practice reversing a singly linked list and see how head, temp, and prev change at every step.

Learning outcomes

  • Track the current node without losing the rest of the list.
  • Understand why prev becomes the new head.
  • Recognize the safe order for pointer rewiring.

What to focus on

Reversal is not about memorizing a pattern. It is about preserving access to the remaining nodes while you redirect the current next pointer. This page keeps the visual state and code trace in sync so the sequence is explicit.

Interview value

This is one of the most common linked list interview exercises because it tests pointer discipline. If you can explain the order of assignments clearly, you usually understand the structure well enough to handle harder variants.

Worked example

  1. Start with A -> B -> C -> D -> E and set prev to null.
  2. Move head one node forward, then point the current node back to prev.
  3. Repeat until head becomes null, then assign prev to head.

FAQ

Why do we need a temp variable?

Without temp, you can lose access to the current node or the remainder of the list while rewiring pointers.

What should the last node point to after reversal?

The original head becomes the last node, so its next pointer must end at null.