TRT
Sign in
easyFoundationscoding

Reverse a linked list, iteratively and recursively

Write both versions, state the loop invariant, and compare their time and space.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstNodes and pointersA linked list is a chain of separately allocated nodes, each holding a value and the address of the next. Walking it is O(n), and rewiring it never moves a value.

A strong answer

A strong candidate frames Reverse a linked list, iteratively and recursively around the exact memory layout, compiler semantics, and hardware execution model covered in Nodes and pointers. Write both versions, state the loop invariant, and compare their time and space.

The first module contrasted contiguous data, where the address of each element is computed, with scattered data, where it is stored in a pointer. A linked list is the simplest scattered structure. Each element lives in its own node, a small object holding two things: the value, and the address of the next node, called next. The last node's next is null, which marks the end. The program keeps one more pointer, the head, which leads to the first node.

Each node is allocated separately, so the nodes can be anywhere in memory. The list's order lives entirely in the next pointers, not in where the nodes sit.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Describe a singly linked list in terms of nodes, next pointers and a head.
  • Mastery of core invariant: State the cost of traversal, search and access by position.
  • Mastery of core invariant: Reverse a list in place by rewiring pointers, keeping the invariant straight.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Reverse only the nodes between positions m and n.
  2. 02Reverse the list in groups of k nodes.
  3. 03How would you reverse a doubly linked list?
  4. 04Why might the recursive version crash on a list of a million nodes?

Not started

Sign in to save your learning progress.

Sign in to save