Coding

Linked List Coding Questions

Linked list problems test pointer manipulation, recursion, and the fast/slow pointer technique. They appear frequently at Meta, Amazon, and Microsoft in both phone screens and onsite rounds.

Sample Questions & Strong Answers

1

Reverse a Linked List (iterative and recursive).

Iterative: prev=null, curr=head. While curr: next=curr.next, curr.next=prev, prev=curr, curr=next. Return prev. Recursive: if !head or !head.next return head. newHead=reverse(head.next), head.next.next=head, head.next=null. Return newHead.

💡

Pro tip: Know both versions. Iterative is O(1) space; recursive is O(n) stack space. Most common linked list question — know it perfectly. Amazon, Meta, Microsoft, Apple.

2

Detect Cycle in a Linked List (Floyd's algorithm).

Fast and slow pointers: slow moves 1 step, fast moves 2. If they meet, cycle exists. To find cycle start: reset slow to head, advance both by 1 until they meet — that's the cycle start. Time O(n), Space O(1).

💡

Pro tip: The cycle start detection is the hard follow-up. Prove it: the meeting point is exactly the cycle start's distance from head. Amazon, Meta, Google.

3

Merge Two Sorted Linked Lists.

Compare heads, attach smaller, recurse or iterate. Iterative: dummy head, tail pointer. While both lists non-null: attach smaller node, advance that list. Attach remainder. Return dummy.next. Time O(m+n), Space O(1).

💡

Pro tip: Dummy head simplifies edge cases (empty list, first node attachment). Recursive is elegant but O(m+n) stack space. Amazon, Meta, Google — warm-up.

4

Find Middle of Linked List.

Fast/slow pointers: slow moves 1, fast moves 2. When fast reaches end, slow is at middle. For even-length list: this returns the second middle (for first middle: stop when fast.next===null or fast.next.next===null). Time O(n), Space O(1).

💡

Pro tip: Clarify: first or second middle for even-length lists. This technique underlies merge sort on linked lists and cycle detection. Amazon, Meta.

5

Remove Nth Node from End of List.

Two pointers: advance fast n steps ahead. Move both until fast reaches end. Slow.next = slow.next.next. Edge case: if fast is null after n steps, remove head (return head.next). Time O(n), Space O(1). Dummy head simplifies removal of head node.

💡

Pro tip: Dummy node prevents special-casing head removal. Count carefully: 'n from end' means fast is n steps ahead when removal happens. Amazon, Google, Meta.

6

Merge K Sorted Lists.

Use a min-heap (priority queue) seeded with each list's head. Extract min, append to result, push that node's next into heap. Time O(N log k) where N=total nodes, k=number of lists. Alternative: divide and conquer merge pairs — same complexity.

💡

Pro tip: Min-heap is the canonical solution. Divide and conquer is elegant recursion. Naive merge-in-series is O(N×k). Amazon, Google, Meta — hard.

7

Copy List with Random Pointer.

3-pass approach: 1) Interleave clones between originals (A→A'→B→B'). 2) Set clone random pointers: clone.random = original.random.next. 3) Separate original and clone lists. Time O(n), Space O(1). Alternatively, use a hash map: O(n) space but simpler code.

💡

Pro tip: The O(1) interleaving trick is impressive. Hash map (original→clone) is much easier to reason about in an interview. Amazon, Meta.

8

LRU Cache — implement O(1) get and put.

Doubly linked list (most→least recently used) + hash map (key → node). Get: move node to front, return value. Put: if exists, update and move to front. If at capacity, remove tail and delete from map. Add new node to front and map. Time O(1), Space O(capacity).

💡

Pro tip: The doubly linked list enables O(1) node removal when given a pointer. Hash map provides O(1) key lookup. Classic design question — implement the full class. Amazon, Meta, Google, Microsoft.

9

Add Two Numbers — add two numbers represented as reversed linked lists.

Iterate both lists simultaneously. At each step: sum = l1.val + l2.val + carry. New node = sum % 10, carry = sum / 10. Advance non-null pointers. After loop, if carry > 0, add one more node. Time O(max(m,n)), Space O(max(m,n)).

💡

Pro tip: Handle lists of different lengths and the final carry cleanly. Dummy head simplifies output list construction. Amazon, Meta, Microsoft.

10

Reorder List — reorder L0→L1→…→Ln to L0→Ln→L1→Ln-1→…

Three steps: 1) Find middle (slow/fast pointers). 2) Reverse second half. 3) Merge two halves by alternating nodes. All three steps are O(n). Total O(n) time, O(1) space.

💡

Pro tip: Breaking this into three clean sub-problems (find-middle, reverse, merge) makes the solution approachable. Meta, Amazon, Microsoft.

Expert tips

1

Always draw the list before coding — pointer reassignment errors are common without a visual.

2

Use a dummy head node to avoid special-casing empty list or head removal.

3

Fast/slow pointer solves: cycle detection, middle finding, kth-from-end in one pass.

4

Recursive solutions for linked lists are elegant but use O(n) stack space — know when this matters.

Related interview topics

Linked List Coding Questions FAQ

Dry-run your pointer logic on a 2-node and 3-node list before submitting. Most off-by-one errors surface on short lists. Use dummy head nodes to eliminate head-node special cases. Carefully reason about when to stop: 'while fast.next !== null' vs 'while fast !== null' changes which node slow lands on.

Iterative is preferred for: reversals (O(1) space), long lists (avoids stack overflow), production code. Recursive is cleaner for: tree-like decomposition (merge K sorted lists), shorter lists, and problems where recursion maps naturally to the structure. In interviews, start with whichever you find clearer, then offer the alternative.

Also called Floyd's tortoise and hare. Slow pointer advances 1 step per iteration, fast advances 2. Used for: detecting cycles (they meet if cycle exists), finding middle (fast at end → slow at middle), finding start of cycle (reset one pointer to head, advance both by 1 until meeting). Time O(n), Space O(1).

Always check node != null before accessing node.next or node.val. Common patterns: 'while (fast != null && fast.next != null)' for safe fast-pointer advancement; check both list pointers in merge loops; check curr.next before setting curr.next.next. Run through null-heavy edge cases: empty list, single node, two nodes.

Practice Linked List Coding Questions with real-time AI.

Stop reading sample answers and start practicing. Get personalized feedback on structure, delivery, and depth — every time.

Free plan available · No credit card needed · Cancel anytime