DSA Tracker

Problem

Easy

Reverse Linked List

An easy Linked List interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Linked List
Sheets
3
Core for
4 roles

The problem

You get the first node of a singly linked list. Turn the list around so the last node comes first and every link points backwards, then return the node that is now at the front.

Example 1

Input
[9,3,7,2]
Output
[2,7,3,9]

Example 2

Input
[1,2]
Output
[2,1]

Example 3

Input
[]
Output
[]

Constraints

  • 0 <= n <= 5000
  • -5000 <= Node.val <= 5000

How to think about it

Updated 2026-09-29

Every node already holds the pointer you want to change, but swinging it backwards orphans the rest of the list before you can reach it. The entire mechanical challenge is stashing the forward neighbour in a temporary variable before rewriting next to point behind you.

Approaches, worst first

  1. Stack of nodes

    time O(n) · space O(n)

    Push every node reference onto an explicit stack, then pop them off in reverse order retying next pointers. It mirrors the reversed sequence immediately, but wastes linear memory for pointer manipulations that require only local context.

  2. Head recursion

    time O(n) · space O(n)

    Recurse to the tail to establish the new head, then on the unwind execute `head.next.next = head` and `head.next = null`. Elegant and minimal, but consumes call stack frames proportional to list length, risking recursion limits.

  3. Three-pointer iterationWrite this one

    time O(n) · space O(1)

    Maintain prev initialized to null, curr at head, and save curr.next into a temporary ahead pointer. Redirect curr.next to prev, advance prev to curr, and advance curr to ahead. Terminate when curr is null and return prev as the brand new head.

Where people lose marks · 3
  • Forgetting to sever the original head's next link causes the first pair to point at one another, introducing an infinite cycle into the returned list.
  • Overwriting curr.next before copying the original forward pointer into a temporary variable permanently detaches the unprocessed tail of the list.
  • Attempting to dereference head.next unconditionally crashes on an empty input list where head starts out as null.

Full solution

Three-pointer iteration is the one to write: O(n) time, O(1) space, no recursion depth risk. Stack and recursion both work but trade memory for no real benefit here.

Python
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def reverse_list(head: Optional[ListNode]) -> Optional[ListNode]:
    prev = None
    curr = head
    while curr is not None:
        ahead = curr.next  # save forward link before it is overwritten
        curr.next = prev
        prev = curr
        curr = ahead
    return prev
JavaScript
class ListNode {
  constructor(val = 0, next = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head) {
  let prev = null;
  let curr = head;
  while (curr !== null) {
    const ahead = curr.next; // save forward link before it is overwritten
    curr.next = prev;
    prev = curr;
    curr = ahead;
  }
  return prev;
}
Try it in the editor

The theory behind it

Linked List — the ground this problem stands on. All Linked List problems

What Linked List is

A linked list is a chain of separate cargo cars connected by coupling hooks, scattered anywhere across memory rather than sitting in a tidy contiguous row. Each car, called a node, holds a single piece of data and a pointer directing traffic to the address of the next car in line. Because nodes connect only by directional links, jumping straight to the tenth car is impossible without walking past the first nine.

When to reach for it

Choose a linked list when a problem requires frequent insertions and deletions at known positions without shifting whole blocks of surrounding memory. Problems mentioning pointer splicing, reversing subsequences in place, merging sorted streams, or detecting cycles in linear chains strongly point here. It is ideal when total capacity is unpredictable and memory allocation must happen one individual node at a time.

How the pattern works

Think in terms of pointer rewiring before dereferencing. Keep a dummy head node pointing to the start of the list so modifications to the initial item do not require separate edge logic. Always save references to neighboring nodes into temporary variables before cutting or redirecting forward links. When diagnosing loops or locating middle nodes, advance two references simultaneously at differing velocities so traversal completes without supplementary storage.

More Linked List problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .