Recommended resources
The freely available resources this lesson will direct you to.
Welcome back! In our last lesson, we built a singly linked list from scratch, implementing the core operations of traversal, insertion, and deletion. You might have noticed a recurring theme: operations involving the head of thelist often required special handling with if/else statements. This is because modifying the head means changing the main entry point to the entire list, which is different from modifying a node in the middle or at the end.
Today, we'll introduce a powerful and elegant technique that eliminates these special cases: the dummy node. This is a simple but profound pattern that cleans up linked list code, making it less error-prone and easier to reason about. By the end of this lesson, you'll be able to use a dummy node to simplify your linked list implementations, a skill that is invaluable for solving interview problems efficiently.
The Problem with the Head
Let's revisit the logic for deleting a node. In the previous lesson, your code likely had a structure similar to this:
- Find the node to delete (
current) and its predecessor (previous). - If
previousisnull, it means we're deleting the head node. In this case, we must update the list'sheadpointer directly:list.head = current.next. - Otherwise, we're deleting a node from the middle or end. We bypass
currentby updating the predecessor'snextpointer:previous.next = current.next.
This branching logic (if (previous === null)) is a common source of bugs. It forces you to handle the head as a separate case for insertions, deletions, and any other modification at the start of the list.
The following video does an excellent job of illustrating this exact issue. Please watch the segment where the presenter first attempts a two-pointer solution and then demonstrates why it fails for the edge case where the node to be removed is at the head.
LEETCODE 203 (JAVASCRIPT) | REMOVE LINKED LIST ELEMENTS
Watch the segment from the two-pointer approach. Pay close attention to the explanation of the edge case where the target value is at the head of the list and why the previous and current pointer setup falls short.
This part of the video explains why a standard two-pointer approach for deletion has a critical weakness when dealing with the first node of the list.
As you saw, the problem is that the head node has no predecessor, forcing us to write special code. What if we could ensure that every node, including the head, always has a predecessor?
The Dummy Node Solution
This is precisely what a dummy node (also known as a sentinel node) accomplishes. It is a placeholder node that we place before the actual head of the list.
Here's how it works:
- Create a new
Node, calleddummy. Its value doesn't matter. - Set
dummy.nextto point to the originalheadof your list. - Perform all your operations starting from this
dummynode.
Now, the original head node has a predecessor: the dummy node. Every single data node in the list now has a node before it. This unifies our logic.
The article "Using Dummy Nodes in Linked List Operations" from Launch School provides a superb, concise explanation of this concept.
Using Dummy Nodes in Linked List Operations
This article introduces the dummy node and contrasts the standard deletion approach with the simplified dummy node approach.
Read the introduction starting from the definition of a dummy node. Then, carefully review the "Example Problem" section, which compares the code for deleting a node with and without a dummy node. Notice how the if (!prev) check disappears in the dummy node version.
The core benefit is that we no longer need to treat the head as a special case. The logic to remove curr is always prev.next = curr.next, regardless of where curr is in the list.
Walkthrough: Removing Elements Using a Dummy Node
Let's apply this to a common interview problem: "Given the head of a linked list and an integer val, remove all the nodes of the linked list that have Node.val == val."
This problem is a perfect showcase for the dummy node pattern because the head itself might be one of the nodes to be removed.
Here is the algorithm using a dummy node:
- Initialize
dummy = new Node()and setdummy.next = head. - Initialize two pointers:
prev = dummyandcurr = head. - Iterate through the list with
curr. - Inside the loop:
- If
curr.val === val, we removecurrby "skipping" it:prev.next = curr.next. Notice thatprevdoes not move forward in this case, because the newcurr(which iscurr.next) might also need to be deleted. - If
curr.val !== val, the node is safe. We advanceprevtocurr:prev = curr. - In either case, we advance
currto the next node:curr = curr.next.
- If
- After the loop finishes, the potentially new head of the list is
dummy.next. Return it.
This process is visualized clearly in the following image. Imagine the dummy node is the cat on the far left, prev starts there, and head is the first node with value 2.

Now, let's watch the implementation of this logic. The same video you watched earlier continues to explain and code this exact solution.
LEETCODE 203 (JAVASCRIPT) | REMOVE LINKED LIST ELEMENTS
Continue watching the video from the dummy node explanation and implementation. The presenter walks through the logic with diagrams and then implements it in JavaScript.
This segment shows how the dummy node solves the head-node problem and provides a line-by-line implementation of the "Remove Linked List Elements" problem.
The corresponding JavaScript/TypeScript code is clean and has no special if conditions for the head.
// Assuming a ListNode class is defined as in the previous lesson
function removeElements(head: ListNode | null, val: number): ListNode | null {
// 1. Create dummy node and link it to the head
const dummy = new ListNode(0); // Value can be anything
dummy.next = head;
// 2. Initialize pointers
let prev = dummy;
let curr = head;
// 3. Traverse the list
while (curr !== null) {
if (curr.val === val) {
// 4a. If value matches, skip the current node.
// prev stays in place.
prev.next = curr.next;
} else {
// 4b. If value doesn't match, move prev forward.
prev = curr;
}
// 4c. Always move curr forward.
curr = curr.next;
}
// 5. The new head is dummy.next
return dummy.next;
}
This pattern is not just for deletion. It's equally useful for any operation that might modify the head, such as insertion or when building a new list from scratch (like in "Merge Two Sorted Lists", LeetCode 21).
For further reinforcement, the Algo.monster article provides another excellent, high-level explanation of this pattern.
707. Design Linked List - In-Depth Explanation
This resource explains the dummy node pattern in the context of designing a linked list from scratch.
Read the "Intuition" section starting from the introduction. Then, review the "Solution Approach" section, which details how the dummy node works for various methods like get, addAtIndex, and deleteAtIndex.
Conclusion
Today you've added a crucial tool to your algorithm toolkit. The dummy node pattern is a testament to how a small change in approach can lead to significantly simpler, more elegant, and more robust code. It's a classic technique that signals a deeper understanding of linked list manipulation.
Here are the key takeaways:
- The "Head" Problem: Modifying the head of a linked list requires special logic because it has no predecessor, leading to brittle
if/elseconditions. - The Dummy Node Solution: By adding a placeholder "dummy" node before the head, we ensure every data node has a predecessor.
- Unified Logic: This allows for a single, unified piece of logic for insertion and deletion, regardless of the node's position.
- The New Head: When using a dummy node, the final head of the list is always
dummy.next.
As you practice more linked list problems, keep an eye out for opportunities to apply this pattern. Anytime an operation might involve the head node, a dummy node should be one of the first things that comes to mind.
In our next lesson, we will tackle another cornerstone linked list problem: reversing a list. This will further hone your pointer manipulation skills, building directly on what you've learned today.