Skip to content
Tech Interview Prep
Technical interview guide

Linked Lists

Singly/doubly linked lists, pointer manipulation, and the classic two-pointer patterns.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v7
Editorial status
Reviewed

Scope: Language-neutral, with container behaviour cited from CPython 3.14, the C++ standard containers, and java.util as of Java 21.

Overview

Curated: · Written: · Reviewed:

Linked Lists

In one line

A linked list gives O(1) insertion and removal at a node you already hold, and nothing else. Reach for one only when the algorithm already holds the node, as in an LRU cache, an intrusive queue or a free list. Otherwise a contiguous array or a deque is the better answer.

Key definitions

  • Held node: a reference you already have. Splicing at it is O(1); reaching position i is O(i).
  • Sentinel (dummy) node: a placeholder before the head, so every splice has a predecessor and the head needs no special case.
  • Runner gap: two pointers a fixed distance apart, used to find the nth node from the end in one pass.
  • Floyd's tortoise and hare: slow moves one node, fast moves two. Inside a cycle they must meet.
  • Intrusive list: the links live inside the object, so insertion allocates nothing, and the object can sit in at most one list per link set.

Formulas and complexity

access at position i=O(i),splice at a held node=O(1)\text{access at position } i = O(i), \qquad \text{splice at a held node} = O(1) reverse: O(n) time;  O(1) space iterative;  O(n) stack recursive\text{reverse: } O(n) \text{ time};\; O(1) \text{ space iterative};\; O(n) \text{ stack recursive} merge two sorted lists of lengths m,n:O(m+n) time,  O(1) extra space\text{merge two sorted lists of lengths } m, n: O(m+n) \text{ time},\; O(1) \text{ extra space} merge sort on a list: O(nlog⁡n) time,  O(log⁡n) stack\text{merge sort on a list: } O(n \log n) \text{ time},\; O(\log n) \text{ stack} cycle detection: O(n) time;  O(1) space (Floyd) vs O(n) space (hash set)\text{cycle detection: } O(n) \text{ time};\; O(1) \text{ space (Floyd) vs } O(n) \text{ space (hash set)}

Worked example (hypothetical arithmetic on the stated figures): 4,000,000 records in a 32-byte node-per-record list, none sharing a cache line, cost about 4,000,000 misses at 80 ns, roughly 320 ms per walk. The same records in a flat 24-byte array are 96 MB, streamed at 16 bytes per ns, roughly 6 ms.

How it works

  • A node holds a value and a next reference. Splicing is a couple of pointer writes.
  • Every operation that changes a link must save the pointer it is about to overwrite first.
  • Boundaries (predecessor, successor) are captured before any link changes.
  • Doubly linked lists fix four links per splice, not two.
map: key to node node A node B node C sentinel

When to use and when not to

SituationLinked listArray or deque
You hold the node and must splice itYesShifts elements
Index access or scanningO(i) per accessO(1) index, prefetchable scan
Memory per elementA pointer plus allocator overhead per nodePacked values
Push or pop at both endsYes, O(1)deque, O(1)
Keyed lookupNodict or hash map

Traps interviewers probe

  1. Quoting O(1) insertion without "at a known node". Finding the position is the linear part. Fix: state both costs.
  2. Redirecting a link before saving next. The loop loses the rest of the list. Fix: save nxt first. Trace 1 to 2 to 3 by hand. Here the buggy version returns only node 1.
  3. Recursive reversal called O(1) space. Each frame holds a node, so stack is O(n) and fails at the recursion limit. Fix: state the space bound and iterate for unbounded input.
  4. Value-copy deletion of a node given only its pointer. It breaks identity for outside references and cannot delete the tail. Fix: track the predecessor, and treat the trick as a puzzle answer.
  5. Comparing values to detect intersection. Equal values are not the junction. Fix: compare with is, and let both walks reach null together when lists do not meet.

Interview tips

Opening. "A linked list gives O(1) splice at a node I already hold. Finding that node is O(n), so I would use it only when the algorithm holds the node."

Follow-ups to expect

  • Why does a dummy head help? It gives every node a predecessor, so one code path covers the head.
  • Why must the fast pointer meet the slow one? Inside a cycle the gap closes by one each step. After meeting, moving one pointer to the head and advancing both by one each step finds the entry node.
  • Why is a doubly linked list plus a hash map O(1) for LRU? The map gives the node, and the list gives O(1) move-to-front and tail eviction.
  • Why do arrays usually win? Contiguous memory is prefetchable, and a linked traversal is a dependent load per element.
  • What fails in a sorted free list? Each sorted insert walks up to the free-list length, so the free path becomes linear in the list size.

30-second answer outline

  1. The structure's real benefit: O(1) splice at a held node.
  2. The cost: O(n) to reach a position, plus a pointer and allocation per element.
  3. The decision: use it for the held-node case, such as LRU; otherwise use an array or deque.
  4. The guard: save links before rewriting them, use a sentinel, and test lengths 0, 1 and 2.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.