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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
Always draw the list before coding — pointer reassignment errors are common without a visual.
Use a dummy head node to avoid special-casing empty list or head removal.
Fast/slow pointer solves: cycle detection, middle finding, kth-from-end in one pass.
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
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