Pep talk

A quick pep talk

Pointers trip everyone up at first. If you've been staring at a nil dereference or a list that mysteriously lost its tail, you're not behind — you're exactly where every programmer has been.

Here's the mindset that helps:

  • Draw it. Sketch the boxes and arrows on paper. Move the arrows as your code runs. Bugs that are invisible in code are obvious in a diagram.
  • Name a node before you rewire it. If you're about to overwrite prev.Next, save what you need first (nxt := prev.Next). Lost references are the #1 cause of linked-list bugs.
  • Check the ends. Empty list? Single node? Operating on the head itself? Those three cases catch most mistakes — a dummy head makes the "head itself" case disappear.
  • Trust the traversal. The for node != nil loop always terminates if you advance node. Most "infinite loops" are a forgotten node = node.Next.

You already have the tools: traverse, build with a dummy, rewire carefully. The rest of this section is practice. Take the harder ones slowly, draw the pictures, and they'll click. Onward.