What is a dummy node?
A dummy node is a placeholder node used as the starting point of a new linked list.
It often has a value like -1 or 0, and its main purpose is not for storing data but to help simplify code logic.
Why use a dummy node?
-
To avoid handling special cases when dealing with the head of the list.
-
To build a new list easily without checking if it’s the first node.
-
To simplify pointer manipulation (especially useful in deletion or partitioning problems).
-
Prevents null pointer exceptions during iteration or insertion.
Common Scenarios / Use Cases
1. Merging Two Sorted Linked Lists
You don’t know which node will be the head of the merged list, so using a dummy node allows you to build the merged list without worrying about head initialization.