THN Interview Prep

206. Reverse Linked List

At a Glance

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

Problem (one-liner)

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

Recognition Cues

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

Diagram

At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.

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

Last updated on

Spotted something unclear or wrong on this page?

On this page