← All Posts
DSA · Linked Lists · Part 28 of 28

Capstone: The Linked List Final Exam

Twenty-seven posts is a lot of reading, and reading is not the same as knowing. This closing post is the check: not "did you follow along?" but "did it stick?" — can you now write the core routines cold, reproduce the complexity table from memory, and recall the handful of one-line facts that decide a whiteboard round? Work through the checklist honestly, reproduce the tables on a blank page, then take the two exams at the bottom. Anything you fumble points you back at exactly one post to reread.

The From-Memory Implementation Checklist

These are the things you should be able to write with no reference, correctly, on the first try. The times are targets for a confident candidate at a whiteboard — not a stopwatch to fear, but a signal: if a row takes you three times as long, that is the post to revisit.

You should be able to write…TargetFrom
A singly linked list with the rule of five (dtor, copy, move)8 minsingly list
Dummy-head insert and delete with no special head case4 minsentinels
Pointer-to-pointer removal (delete the head with no branch)4 mintraversal
Iterative reversal (three-pointer loop)3 minreversal
Recursive reversal, and stating its O(n) stack cost4 minreversal
Reverse between positions m and n in one pass6 mink-group
Reverse in k-groups, remainder left intact8 mink-group
Find the middle, both midpoint conventions3 minfast & slow
n-th node from the end, single pass4 minfast & slow
Floyd cycle detection and the entrance6 mincycle detection
Merge two sorted lists with a dummy tail4 minmerging
Merge k sorted lists (heap or divide and conquer)8 minmerging
Bottom-up merge sort in O(1) extra space10 minsorting
Partition around a value, terminating the tail5 minpartitioning
Reorder L0→Ln→L1→Ln-1…7 minreordering
Rotate right by k (with k reduced mod length)5 minrotation
Palindrome check in O(1) extra space6 minrecursion
Clone a list with random pointers by weaving8 minclone
An LRU cache: hash map + doubly linked list10 minLRU cache
Skip-list search across levels8 minskip list
Intrusive container_of from a member pointer6 minintrusive
How to use this honestly. Do not read the rows and nod. Open a blank editor, pick a row at random, and write it with autocomplete off. The gap between "I recognise this" and "I can produce this cold" is the entire difference between passing and failing an interview, and it is invisible until you try.

Rapid Recall

Step the card. It shows an operation; press again to reveal the optimal approach and its complexity. Cover the screen and answer before you flip — that retrieval effort is what moves it into long-term memory.

▶ Operation → Approach → Complexity

Ten canonical operations as flashcards. Step reveals the back of the current card, then advances to the next. It cycles, so you can drill it as long as you like.

The Complexity Table to Reproduce Cold

Write this from memory on a blank page. If any row surprises you, that operation is not yet automatic. The gotcha column is the detail interviewers probe once you state the headline numbers.

OperationTimeExtra spaceThe gotcha
Reverse entire listO(n)O(1)Recursive form is O(n) stack, not O(1).
Find middleO(n)O(1)The loop condition decides first vs second middle.
Detect cycle + entranceO(n)O(1)Reset one pointer to head after the meet.
n-th node from the endO(n)O(1)Anchor on a dummy so removing the head is free.
Merge two sortedO(n + m)O(1)Attach the non-empty remainder at the end.
Merge k sortedO(N log k)O(k)Folding one at a time is O(kN) — avoid it.
Sort a list (merge sort)O(n log n)O(1)Bottom-up is O(1) space; top-down costs O(log n) stack.
Partition around xO(n)O(1)Terminate the second sublist or you build a cycle.
Reorder L0→Ln→L1…O(n)O(1)Three moves: middle, reverse tail, interleave.
Rotate right by kO(n)O(1)Reduce k mod length before cutting.
Palindrome checkO(n)O(1)Restore the reversed half if asked to preserve input.
Clone with random pointersO(n)O(1)Interleave clones, wire randoms, then unweave.
LRU cache get / putO(1)O(capacity)Needs a doubly linked list; singly cannot unlink in O(1).

Ten One-Line Facts That Decide a Round

Each of these has ended (or saved) a whiteboard answer. Commit them to memory verbatim.

  1. You cannot unlink a node from a singly linked list in O(1) without its predecessor — unless you cheat by copying the next node's value in and deleting that.
  2. splice between std::lists is O(1), but std::list::size() is O(1) only since C++11; older code walked the list to count.
  3. Merge sort is the list sort: O(n log n), sequential access only, and O(1) extra space in the bottom-up form.
  4. Reversal is O(1) space iteratively but O(n) stack recursively — the recursive one overflows on long lists.
  5. In Floyd's algorithm, the head-to-entrance distance equals the meeting-point-to-entrance distance, which is why resetting one pointer to the head works.
  6. A dummy head removes the "delete the first node" special case from every deletion routine.
  7. Fast/slow with while (fast && fast->next) lands the slow pointer on the second of two middles for even lengths.
  8. An accidental cycle from a missing null-terminate hangs, it does not crash, and no memory sanitizer catches it — only a Floyd check does.
  9. An LRU cache needs a doubly linked list; a singly linked list cannot unlink an arbitrary used node in O(1).
  10. Copying an owning list with the compiler-generated copy constructor double-frees — obey the Rule of Three/Five.

Final Exam, Part A — Modules 1–2

Fundamentals, complexity, and pointer surgery. Seven questions; aim for all seven before you look anything up.

Final Exam, Part B — Modules 3–4

Advanced structures, caches, concurrency, and memory safety. Six questions.

Where to Go Next

Linked lists are the on-ramp to every pointer-based structure. The natural continuations in this collection:

Practice

The final drill is not more reading — it is production under mild pressure. Do these and you are done.