Linked list interview questions
Linked list questions test pointer discipline. The algorithms are short; the bugs come from losing a reference, forgetting the end of the list, or editing links in the wrong order.
How do I solve linked list interview questions?
Linked list problems are solved by careful pointer manipulation: reverse by re-pointing each node, find a middle or a cycle with fast and slow pointers, and merge or delete with a dummy head that removes special cases. Draw the list, move one pointer at a time and test an empty and a single-node list.
How to recognise a linked list problem
The input is a singly linked list and the question is to reverse, merge, reorder, remove a node, detect a cycle or find the middle.
You are asked to work in place with constant extra space.
A solution that copies the values into an array exists, and the interviewer wants the pointer version.
How to build the solution
Draw three or four nodes and name your pointers: previous, current, next. Before you change current.next, save it, or you lose the rest of the list.
To reverse: loop while current is not null, save next, point current at previous, advance previous and current. Return previous.
For the middle or a cycle, move a slow pointer one step and a fast pointer two steps. The slow one is at the middle when the fast one reaches the end; they meet if there is a cycle.
Use a dummy head node when the head itself might change, such as when deleting or merging, and return dummy.next.
Classic problems to practise
Reverse Linked List, Merge Two Sorted Lists, Linked List Cycle, Remove Nth Node From End of List, Reorder List, Add Two Numbers, Copy List with Random Pointer, Merge k Sorted Lists.
Mistakes interviewers see
Losing the rest of the list by overwriting next before saving it.
Dereferencing null: the end of the list, an empty list and a one-node list are the standard tests, so run them yourself.
Forgetting to set the new tail's next to null, which leaves a cycle.
Skipping the dummy head and then writing a special case for the first node in three places.
Practise it as an interview
Take a topic-based interview on linked list to practise the pattern the way it is asked: say the pattern before you code, write and run the solution in a live editor, and answer the cross-questions about edge cases and complexity. The scored report shows whether you lost points explaining, coding or defending. Your first 3 single-question interviews are free for life, every interview report is free, and a new account also gets 6 welcome credits.
Frequently asked questions
Why use a dummy head?
It gives the list a node before the real head, so inserting or deleting at the front follows the same code as everywhere else.
How do I find the start of a cycle?
After the fast and slow pointers meet, restart one at the head and move both one step at a time. They meet again at the cycle's first node.
Keep reading
- Arrays and hashing interview questions
- Two pointers interview questions
- Sliding window interview questions
- Stack interview questions
- Binary search interview questions
- Trees interview questions
- Graphs interview questions
- Heap and priority queue interview questions
- Company interview guidesRounds and questions by company