Course contentsShow
← Computer Science
Lesson 334 of 2,8728. Linked Data StructuresFree lesson

Deleting a Specific Node

Finding and removing a node by value or position; updating the predecessor's next pointer.

Deleting a Specific Node

You'll learn how to find and remove a particular node from a linked list, whether by its value or position, and properly reconnect the remaining nodes.

The Core Challenge

Deleting a specific node means finding it first, then carefully "unhooking" it from the chain. Unlike deleting from the head or tail (which have fixed positions), you must traverse the list to locate your target.

The key: You need to track the node before the one you want to delete (the predecessor), because you'll need to update its next pointer to skip over the deleted node.

Two Common Approaches

Delete by value: Traverse the list comparing each node's data. When you find a match, remove that node.

Delete by position: Traverse to the nth node and remove it.

The Process (Illustrated)

Imagine a chain: A → B → C → D, and you want to delete C.

  1. Start at the head (A)
  2. Move forward, keeping track of the previous node
  3. When you find C, update B's next pointer to point to D instead
  4. Deallocate C's memory

The chain becomes: A → B → D

Edge Cases to Handle

  • Node not found: Traverse completes without finding the target
  • Deleting the head: Use your existing "delete from head" logic (no predecessor exists)
  • Empty list: Nothing to delete

Why the Predecessor Matters

Without tracking the previous node, you'd have no way to "bypass" the node you're deleting. The predecessor's next pointer is your bridge across the gap.

Key Takeaway: Deleting a specific node requires finding it by traversing the list, keeping track of the predecessor node, then updating the predecessor's next pointer to skip over the deleted node before deallocating its memory.