Linked list edge cases I kept getting wrong
A checklist I now run through for every linked list method:
- Empty list: does head (and tail) stay null correctly?
- One element: removing it must clear both head AND tail.
- Adding to the front of an empty list: tail needs to be set too.
- Removing the last node in a singly linked list: you need the node BEFORE it, so you walk the list.
- Size: update it in every path, including early returns.
- Doubly linked: every change touches two pointers. Draw the arrows before writing code.
Nearly all of my failed tests traced back to one of these.
Comments
"Removing it must clear both head AND tail" cost me so many test cases.