Linked Lists
Reverse, merge, split and detect cycles in singly and doubly linked lists using dummy nodes and slow and fast pointers.
Beginner · about 7 hours · difficulty 2/5
Learn first: Recursion Fundamentals
Linked list questions test careful pointer handling more than clever ideas. They are very common in first technical rounds at both service and product companies.
Core techniques
- Dummy (sentinel) node: removes special cases when the head might change.
- Iterative reversal with prev, curr and next. Also know the recursive version.
- Slow and fast pointers: middle of the list, cycle detection (Floyd's), start of the cycle, nth node from the end (gap of n).
- Merge: two sorted lists; then Sort List using merge sort.
- Splitting and reconnecting: Reorder List (find middle, reverse second half, merge), Reverse Nodes in k-Group.
- Hash map of nodes: Copy List with Random Pointer, LRU Cache (hash map + doubly linked list).
Classic problems
Reverse Linked List, Middle of the Linked List, Merge Two Sorted Lists, Linked List Cycle and Cycle II, Remove Nth Node From End, Palindrome Linked List, Intersection of Two Linked Lists, Add Two Numbers, Reorder List, Reverse Nodes in k-Group, LRU Cache.
Pitfalls
- Losing the rest of the list by overwriting next before saving it.
- Null pointer access on fast.next.next. Check fast and fast.next.
- Not handling lists of length 0, 1 and 2.
You are ready to move on when you can reverse a list iteratively and recursively and implement LRU Cache with O(1) get and put.
Key concepts
- Singly and doubly linked lists
- Dummy nodes
- Reversal: iterative and recursive
- Slow and fast pointers
- Floyd's cycle detection
- Merging and sorting lists
- LRU Cache design
Mini project
Solve these 10 in 7 days, log time and pattern: Reverse Linked List, Middle of the Linked List, Merge Two Sorted Lists, Linked List Cycle II, Remove Nth Node From End of List, Palindrome Linked List, Add Two Numbers, Reorder List, Copy List with Random Pointer, LRU Cache.
Check yourself
After the slow and fast pointers meet inside a cycle, why does moving one pointer back to the head and stepping both by one find the cycle start?
Free resources
- VisuAlgo: linked lists (VisuAlgo)
- NeetCode roadmap: Linked List (NeetCode)
- LeetCode problem set (LeetCode)