DSA Master › Topic 7
Linked Lists
Linked list problems are mostly careful pointer bookkeeping. Three tools cover nearly all of them: a dummy head node so edge cases go away, slow/fast pointers for the middle or a cycle, and in-place reversal.
Concepts
- Dummy nodes
- Slow/fast pointers
- Reversal
- Pointer manipulation
- Cycle detection
How to recognise it
- You need the middle, the nth node from the end, or to detect a cycle. Use slow/fast pointers.
- The head itself might change. Use a dummy node.
- The problem asks you to reverse, reorder or merge lists.
Common pitfalls
- Save next before you rewire cur.next.
- Draw the pointers on paper for k-group reversal. It's easy to lose a link.
Problems
- Reverse Linked List (Easy)
- Linked List Cycle (Easy)
- Linked List Cycle II (Medium)
- Middle of the Linked List (Easy)
- Merge Two Sorted Lists (Easy)
- Remove Nth Node From End of List (Medium)
- Add Two Numbers (Medium)
- Reorder List (Medium)
- Reverse Nodes in k-Group (Hard)