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.
Linked list practice questions
Write a function to delete a node from a linked list. Turns out you can do it in constant time! keep reading »
Does This Linked List Have A Cycle? »Check whether a singly linked list has a cycle, using only O(1) additional space. A classic coding interview question, with a step-by-step solution. keep reading »
Reverse A Linked List »Reverse a singly linked list in place and return the new head, in O(n) time and O(1) space. A classic interview question, solved step by step. keep reading »
Kth to Last Node in a Singly-Linked List »Find the kth to last node in a singly linked list in O(n) time and O(1) space, a cousin of removing the nth node from the end. Step-by-step solution. keep reading »
Cake Photo Cache »Build a fixed-size photo cache that evicts the least recently used photo, with O(1) get and put: the LRU cache interview question, step by step. keep reading »
Find Repeat, Space Edition BEAST MODE »Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »
Keep going with the Linked lists section of our coding interview course.
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?
How do you detect a cycle in a linked list?
How do you reverse a linked list?
When should you use a linked list instead of an array?
What is the two-pointer (runner) technique for linked lists?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake