Free sample lesson ยท Linked List Interview Patterns

Pointer Tracing

Why does one misplaced next pointer make half your list disappear?

Lesson 1 of the Wondering course Linked List Interview Patterns

One Arrow Holds the Rest

You already know each node holds a value and a next reference. Here is the part that bites in interviews.

Often, the only way to reach the rest of the list is through that one next reference.

If you overwrite next before saving where it pointed, every node after it can no longer be reached. Half your list can vanish in one line.

A linked list where node 1's next arrow is redirected to null, cutting off nodes 2 and 3

Notice that nodes 2 and 3 still exist, but once node 1's arrow points to null, no arrow leads to them anymore.

Pointer tracing is how you catch this before it happens.

How to Trace on Paper

Pointer tracing means drawing the list and updating the drawing as your code runs.

Draw every node as a box with an arrow for next. Label the variables that refer to nodes, such as head, prev, curr, and next.

Then redraw the picture after each assignment.

The tracing routine
  • Draw boxes: Draw every node as a box.
  • Add arrows: Draw an arrow for each next reference.
  • Label variables: Mark where head, prev, curr, and next point.
  • Run one line: Apply a single assignment from your code.
  • Redraw: Draw the new picture before the next line.

One line of code, one new picture. That is the whole habit.

The Question Before Every Change

Before you change any next arrow, stop and ask one question.

Will I still be able to reach the node it points to now?

If yes, go ahead. If not, save that node in a temporary variable first, then change the arrow.

Before changing a next arrow
  • About to change next
  • Still reachable after?: Can I still reach the node next points to now?
  • Save in temp: Store that node in a temporary variable.
  • Change the arrow
  • Redraw picture
  • Yes
  • No

This habit prevents the most common linked list bug: a next link overwritten before it is saved.

Spot the Bug in Reversal

Imagine an internship interview. You are reversing 1 -> 2 -> 3 -> null, starting with prev = null and curr = node 1.

Buggy order: you write curr.next = prev first. Node 1 now points to null.

Now nothing points to node 2. Nodes 2 and 3 are lost.

Correct order: save next = node 2, set node 1's next to null, then set prev = node 1 and curr = node 2.

Same first step, two orders
  • Buggy order
  • Correct order

Trace the Whole Reversal

Repeat the correct steps and redraw after each round. Every round saves next, flips one arrow, then moves prev and curr forward.

Round 2 saves node 3, points node 2 at node 1, and moves curr to node 3.

Round 3 saves null, points node 3 at node 2, and curr becomes null. The loop stops.

Result: 3 -> 2 -> 1 -> null, with no nodes lost.

Reversing 1 -> 2 -> 3 -> null

Trace the Edge Cases Too

Check edge cases with the same drawings. Three deserve a picture every time.

An empty list. A single node. And the last node, where next is null.

Three cases to draw
  • Empty list
  • Single node
  • Last node

For each one, redraw after every assignment and ask the same question: can I still reach every node I need?

Interviewers often test these cases first, so trace them before you say you are done.

Why Drawing Pays Off

In an interview, talking through your drawings shows the interviewer how you reason.

Tracing also makes later patterns easier to get right: two pointers, reversal, merging, cycle detection, and dummy nodes.

Each of them changes pointers, so each can lose nodes the same way.

Your takeaway: draw first, ask the reachability question, save before you overwrite, then code.

Is drawing too slow for interviews?

Check yourself

A list is A -> B -> C -> null, and A.next is the only reference to node B. You run A.next = null without saving B anywhere. Can your code still reach nodes B and C?

  • Yes
  • No

Answer: No

No. A.next was the only path to B, and B.next is the only path to C. Once A.next is overwritten, nothing points to B, so B and C are cut off and lost. Saving B in a temporary variable first would have kept them reachable.

Complete the flowchart for safely modifying linked list pointers.

Answer: Save in temp, Redraw picture

If a node will not be reachable after redirecting an arrow, save it in a temporary variable first. After updating the pointer, redraw the diagram to reflect the new state.

Tap the line where nodes are cut off and lost because a next pointer is overwritten before being saved.

Answer: curr.next = prev

Setting curr.next = prev before saving curr.next in a temporary variable severs the connection to the rest of the list, permanently losing access to node 2 and beyond.

Keep going

This is lesson 1 of 13 in Linked List Interview Patterns. Next up:

  1. Dummy Nodes
  2. Edge Cases
  3. Fast-Slow Pointers
  4. Cycle Detection
  5. Nth From End

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.