~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Linked lists
Dummy heads, pointer rewiring, fast/slow pointers.
Notes
Recognise it when: you're given ListNodes. Expect pointer rewiring with O(1) extra space.
def reverse(head):
prev = None
while head:
head.next, prev, head = prev, head, head.next
return prev
- A dummy head (
dummy = ListNode(0, head)) removes the special cases for the first node. - Fast/slow pointers: the middle (slow ends there), cycle detection (they meet), the cycle start (reset one to head and step both by 1), and the nth from the end (move fast n ahead first).
- Merge two sorted lists with a tail pointer.
Gotchas: save next before you rewire. Check fast and fast.next. Draw it.
13 problems
Interview roadmap
Linked lists Reverse, merge, detect cycles, build an LRU cache.
Linked lists guide
- Basics: middle node with fast and slow pointers basics py · c++ · java easy
- Shunt the extra wagons onto a siding py · c++ · java easy
- Reverse Linked List py · c++ · java easy
- Merge Two Sorted Lists py · c++ · java easy
- Remove Nth Node from End of List py · c++ · java easy
- Where does the cycle start? medium
- Reorder List py · c++ · java medium
- Copy List with Random Pointer medium
- Add Two Numbers py · c++ · java medium
- Find the Duplicate Number py · c++ · java medium
- Reverse Nodes in k-Group py · c++ · java hard
- Reverse Linked List II py · c++ · java medium
- Design Circular Queue py · c++ · java medium