Linked List
Linked lists are linear data structures where elements (nodes) are connected via pointers. Unlike arrays, they provide O(1) insertion/deletion but O(n) access. Mastering pointer manipulation is key to solving these problems.Core Concepts
Linked List Types
- Singly Linked: Each node has
nextpointer - Doubly Linked: Each node has
nextandprevpointers - Circular: Last node points back to first
Key Operations
- Traversal: Follow
nextpointers - O(n) - Insertion: Update pointers - O(1) if at known position
- Deletion: Update pointers, handle prev node - O(1) if at known position
- Search: Linear scan - O(n)
Key Patterns & Techniques
1. Two Pointers (Fast & Slow)
- Detect cycle (Floyd’s algorithm)
- Find middle node
- Find kth from end
- Check if palindrome
2. Dummy Node
- Simplifies edge cases (empty list, single node)
- No special handling for head
- Return dummy.next as new head
3. Reverse Linked List
- Iterative with 3 pointers (prev, curr, next)
- Recursive with return new head
- Reverse in groups
4. Merge & Sort
- Merge two sorted lists
- Merge k sorted lists (heap/divide & conquer)
- Sort linked list (merge sort)
Common Approaches
Two Pointers Template (Fast & Slow)
Cycle Detection
Remove Nth From End
Reverse Linked List
Copy List with Random Pointer
Add Two Numbers
Insert into Sorted Circular List
Problems in This Category
045. Remove Nth Node From End
Medium | Frequency: 56.0%Two pointers with n gap - find nth from end in one pass.
032. Copy List with Random Pointer
Medium | Frequency: 67.4%Two-pass with hash map to handle random pointers.
054. Add Two Numbers
Medium | Frequency: 47.0%Traverse both lists simultaneously, handle carry.
064. Insert into Sorted Circular List
Medium | Frequency: 47.0%Handle three cases: middle insert, boundary, all same values.
Complexity Characteristics
| Operation | Array | Singly Linked | Doubly Linked |
|---|---|---|---|
| Access | O(1) | O(n) | O(n) |
| Insert (beginning) | O(n) | O(1) | O(1) |
| Insert (end) | O(1) amortized | O(n) without tail | O(1) with tail |
| Insert (middle) | O(n) | O(1) with node | O(1) with node |
| Delete (beginning) | O(n) | O(1) | O(1) |
| Delete (middle) | O(n) | O(1) with prev | O(1) with node |
| Search | O(n) | O(n) | O(n) |
Interview Tips
Pattern Recognition
- “Detect cycle” → Fast/slow pointers (Floyd’s algorithm)
- “Find middle” → Fast moves 2x, slow moves 1x
- “Nth from end” → Two pointers with gap of n
- “Reverse list” → Three pointers (prev, curr, next)
- “Merge sorted lists” → Two pointers, dummy node
- “Palindrome check” → Find middle, reverse second half, compare
Common Mistakes
- Losing reference to next node before updating pointer
- Not handling empty list or single node
- Forgetting to move pointers forward (infinite loop)
- Off-by-one errors with nth from end
- Not using dummy node (complicates edge cases)
- Memory leaks (not breaking cycles when needed)
Key Insights
- Dummy node eliminates edge cases - especially for head changes
- Draw diagrams - visualize pointer changes
- Two pointers solve many problems in O(1) space
- Save next before changing pointers - common bug source
- Check null pointers before accessing
.next - Circular lists need extra care to detect when traversal completes
Essential Techniques
1. Runner Technique (Fast/Slow)
2. Dummy Node Technique
3. Two Pointers with Gap
Advanced Patterns
Merge K Sorted Lists
Sort Linked List (Merge Sort)
Palindrome Check
Flatten Multilevel Doubly Linked List
Linked List vs Array
| Aspect | Array | Linked List |
|---|---|---|
| Memory | Contiguous | Scattered |
| Access | O(1) | O(n) |
| Insert/Delete (beginning) | O(n) | O(1) |
| Insert/Delete (end) | O(1) amortized | O(1) with tail pointer |
| Memory overhead | None | Extra for pointers |
| Cache performance | Better | Worse |
Pro Tip: Always use a dummy node for problems that might change the head. It eliminates special cases and makes code cleaner. Draw pointer diagrams to visualize changes before coding.