Linked list coding interview questions
- 01Fast and slow pointers3 questions
- 02Dummy head and pointer rewiring
- 03Merge two sorted sequences3 questions
- 04In-place linked list reversal
- 05Copy with extra pointers
Fast and slow pointers
Two pointers moving at different speeds, or two walks that switch heads, to find a cycle, a cycle start or an intersection.
1.1 Linked List Cycle Easy 3 min
I send a slow pointer one step at a time and a fast pointer two steps. If there is a cycle they will meet; if the fast pointer reaches null there is no cycle. The algorithm uses constant extra memory and does not change the list.
Read the full answer1.2 Intersection of Two Linked Lists Easy 4 min
The two lists may share a common tail. I walk one pointer down each list, and when a pointer reaches the end I send it to the head of the other list. They travel the same total length and therefore meet at the first shared node, or both land on null if the lists do not intersect.
Read the full answer1.3 Linked List Cycle II Medium 4 min
I first find a meeting point with the slow and fast pointers, as in the cycle-detection problem. Then I put one pointer back at the head and walk both one step at a time; they meet at the start of the cycle. If the fast pointer never meets the slow pointer, I return null. This problem is named in a public Microsoft interview report.
Read the full answer
Dummy head and pointer rewiring
A dummy node in front of the real head, so deleting or building from the front needs no special case.
2.1 Remove Nth Node From End of List Medium 4 min
I put a dummy head in front, then send a fast pointer n + 1 steps ahead of a slow pointer. When fast reaches the end, slow sits just before the node I need to drop, so I can skip it. The dummy head means deleting the original head needs no special case.
Read the full answer
Merge two sorted sequences
Attach the next node by a comparison or by a carry, then splice whatever remains.
3.1 Merge Two Sorted Lists Easy 3 min
Both lists are already sorted, so I attach the smaller current head to a dummy node and advance that list. When one list runs out I attach the rest of the other. A dummy head saves a special case for the first node, and the merge is in place on the existing nodes.
Read the full answer3.2 Add Two Numbers Medium 4 min
The digits are stored in reverse order, one per node, so I can add from the heads and carry forward just as I would on paper. I walk both lists with a dummy head, write sum modulo 10 as the next node, and keep sum / 10 as the carry. A leftover carry after both lists end becomes a new node.
Read the full answer3.3 Sort List Medium 4 min
I merge-sort the list so the sort stays O(n log n) time and O(1) extra memory besides the recursion, or O(log n) stack. I split the list in half with slow and fast pointers, sort each half, and merge the two sorted lists with the same dummy-head merge used earlier.
Read the full answer
In-place linked list reversal
Reverse a bounded span, or reverse the second half so you can interleave it with the first.
4.1 Reverse Linked List II Medium 4 min
I walk to the node just before position left, then I reverse the next right minus left + 1 nodes by repeatedly taking the following node and moving it to the front of that span. A dummy head covers the case where left is 1, so the original head may change.
Read the full answer4.2 Reorder List Medium 4 min
The asked order is first, last, second, second last, and so on. I find the midpoint with slow and fast pointers, split the list, reverse the second half, and then merge the two halves by alternating nodes. The first half is the longer one when the length is odd, so the leftover middle node stays in place.
Read the full answer
Copy with extra pointers
A map from original node to clone, so random pointers can be rewritten onto the copy.
5.1 Copy List with Random Pointer Medium 4 min
Each node has a next pointer and a random pointer that may point at any node or at null. I first walk the list and put a copy of every node in a map from original to clone. In a second walk I set each copy's next and random from the map, so the references go to the cloned nodes rather than the originals.
Read the full answer