THN Interview Prep

206. Reverse Linked List

Quick Identifier

  • Topic: linked-list
  • Pattern: In-place Linked List Reversal
  • Difficulty: Easy
  • Companies: Amazon, Google, Meta, Bloomberg, Microsoft
  • Frequency: Very High
  • LeetCode: 206

Quick Sights

  • Time Complexity: O(n) from the optimal approach.
  • Space Complexity: O(1) from the optimal approach.
  • Core Mechanism: Given the head of a singly linked list, reverse every pointer direction and return the new head (previously the tail).

Concept Explanation

Given the head of a singly linked list, reverse every pointer direction and return the new head (previously the tail).

The key is to state the invariant before coding: what part of the input is already settled, what state is still unresolved, and what single operation makes progress without losing correctness.

Recognition cues:

  • “Reverse a linked list”
  • In-place, O(1) extra space
  • Iterative or recursive reconstruction of Next fields

Study Pattern (SDE3+)

  • 0-3 min: restate I/O and brittle edges aloud, then verbalize two approaches with time/space before you touch the keyboard.
  • Implementation pass: one-line invariant above the tight loop; state what progress you make each iteration.
  • 5 min extension: pick one constraint twist (immutable input, stream, bounded memory, parallel readers) and explain what in your solution breaks without a refactor.

Diagram

This diagram shows the algorithm movement for this problem family.

Loading diagram…

Approaches

  • Brute forceO(n) time / O(n) space — build a new list or copy to slice then relink.
  • Better — same as optimal for this task.
  • OptimalO(n) time / O(1) space — three pointers: previous, current, next to relink in one pass.

Optimal Solution

Go

type ListNode struct {
	Val  int
	Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
	var previous *ListNode
	current := head
	for current != nil {
		next := current.Next
		current.Next = previous
		previous = current
		current = next
	}
	return previous
}

JavaScript

class ListNode {
	constructor(val = 0, next = null) {
		this.val = val;
		this.next = next;
	}
}

function reverseList(head) {
	let previous = null;
	let current = head;
	while (current !== null) {
		const next = current.next;
		current.next = previous;
		previous = current;
		current = next;
	}
	return previous;
}

Walkthrough

List 1 -> 2 -> 3 -> nil

steppreviouscurrentnextaction
initnil12
11231.Next = nil
223nil2.Next = 1
33nilreturn previous=3

Result 3 -> 2 -> 1. Invariant: reversed prefix hangs off previous.

Edge Cases

  • Empty list — nil head.
  • Single node — returns same node with Next nil.

Pitfalls

  • Losing the rest of the list — stash next before rewriting current.Next.
  • Returning old head instead of new head (previous after loop).

Similar Problems

Variants

  • Reverse between indices — pattern becomes range reversal.
  • Recursive version — O(n) call stack; elegant but less space-optimal.

Mind-Map Tags

#linked-list #in-place #three-pointer #reversal

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page