Pointer Manipulation Resource - aloalgo

Pointer Manipulation

Many linked list problems require re-organizing pointers - changing which nodes point to which. The key is to carefully track references before overwriting them.

The Golden Rule

Before overwriting a pointer, save any references you'll need later. Once you overwrite node.next, the original next node is lost unless you saved it.

The Dummy Node Technique

A dummy node is a temporary node placed before the head. It simplifies edge cases when the head might change or when building a new list.
Use a dummy node when:
  • The head node might be removed
  • Inserting at the beginning of the list
  • Building a new list from scratch

Remove Elements

Remove all nodes with a given value. A dummy node eliminates special handling when removing the head.

Delete Nth Node from End

Use two pointers with a gap of n nodes. When the first pointer reaches the end, the second is just before the node to delete.

Merge Two Sorted Lists

Idea: Use a dummy node to build the result. Compare the heads of both lists, append the smaller one to the result, and advance that list's pointer.
  • Time: O(n + m) where n and m are the list lengths
  • Space: O(1)

Reverse a Linked List

Idea: Iterate through the list, reversing each pointer. Keep track of three nodes: previous, current, and next. Point current's next to previous, then advance all three.
  • Time: O(n)
  • Space: O(1)

Common Interview Tips

  1. Draw it out: Sketch the before and after states with arrows showing pointer changes.
  2. Save before overwriting: If you need a reference later, save it to a temp variable first.
  3. Use dummy nodes: They simplify edge cases when the head might change.
  4. Return dummy.next: Always return dummy.next, not head or dummy.
Was this helpful?
© 2026 aloalgo. All rights reserved.