Linked List Interview Questions

How to approach linked list questions

Linked list questions test whether you can move pointers around without losing track of a node. Interviewers watch for the edge cases: an empty list, a list with one node, and the head or the tail changing.

Most linked list problems come down to a few techniques: walking the list with two pointers (one fast and one slow, or one a fixed distance ahead of the other), reversing next pointers as you go, and holding on to the previous node so you can link around a node you remove. These usually give O(n) time and O(1) extra space, which is often what the interviewer is after.

Before you code, draw a short list and trace your pointers through it by hand. The usual bugs are a lost reference and an off-by-one at one of the ends.

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Linked lists: How nodes and next pointers work, and how linked lists compare with arrays.
  • Arrays: The main alternative: O(1) lookups by index, but O(n) inserts in the middle.
  • In-place algorithms: Changing the input with O(1) extra space, the usual goal for linked list problems.
  • Hash tables: A quick first approach for many list problems, like remembering the nodes you've already seen.
  • LRU cache: A classic design question built from a hash map and a doubly linked list.

Quick reference: Data structures cheat sheet

Linked list interview FAQ

What are the most common linked list interview questions?

Classic ones include reversing a linked list, detecting a cycle, finding the kth-to-last node, deleting a node, merging two sorted lists, and finding where two lists intersect. Most are solved by walking the list with one or two pointers.

How do you detect a cycle in a linked list?

Start two pointers at the head: a slow one that moves one node at a time and a fast one that moves two. If there's a cycle, the fast pointer eventually catches up with the slow one; if the fast pointer reaches the end of the list, there's no cycle. This takes O(n) time and O(1) space.

How do you reverse a linked list?

Walk the list once, keeping track of the previous node, and point each node's next pointer back at the previous node as you go. When you reach the end, the last node you visited is the new head. This takes O(n) time and O(1) extra space.

When should you use a linked list instead of an array?

Use a linked list when you need fast inserts and deletes at the front, or next to a node you already have, since those take O(1) time without shifting other items. Use an array when you need fast lookups by index, which a linked list can only do in O(n) time.

What is the two-pointer (runner) technique for linked lists?

It means walking a linked list with two pointers, either at different speeds or with a fixed gap between them. A gap of k nodes finds the kth-to-last node in one pass, and a fast pointer that moves twice as fast as a slow one finds the middle of the list or detects a cycle.
. . .