← All Lessons Lesson 3 / 80
Lesson 3

Defining a Node in a Singly Linked List

What is a Node?

Think of a linked list like a treasure hunt. At each spot you find two things: a small piece of treasure, and a clue that tells you where to go next. A node is one of those spots.

A node is the basic building block of a linked list. Every node holds two things:

  • The actual data (the treasure).
  • A pointer to the next node (the clue that says where to go next).

When you join many nodes together — each one pointing to the one after it — you get a single linked list. Everything you do with a linked list (adding items, removing items, or changing items) is really just working with these individual nodes and the links between them.

The Two Parts of a Node

A singly linked list node has exactly two sections:

  • val — the actual data the node stores. This can be any type: a number, a letter, a name, anything.
  • next — a reference (a pointer) to the next node in the list. It does not hold the next node itself; it just *points* to where the next node lives.

You can picture one node like this:

[ val | next ]

The left box holds the data, and the right box holds the link to the node that comes after it. If a node is the last one in the list, its next points to "nothing" (we will see this written as nullptr in C++).

Writing a Node in Code

To create a node in a program, we write a small class (a blueprint) that bundles together the two pieces of information a node needs: the data and the link to the next node.

A good blueprint also comes with a constructor. A constructor is special code that runs the moment a new node is created, and it sets up the starting values. This way, when you make a node, its val and next already have sensible values instead of being empty garbage.

Here is the C++ version:

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int val) : val(val), next(nullptr) {}
};

Let's read this line by line:

  1. struct ListNode { ... } — this is the blueprint. Every node we make from it is a ListNode.
  2. int val; — the data part. Here it stores an integer.
  3. ListNode *next; — the link part. The * means it is a pointer to another ListNode (the next one in the list).
  4. ListNode() : val(0), next(nullptr) {} — the first constructor. If you make a node without giving any data, it starts with val equal to 0 and next set to nullptr (meaning "points to nothing").
  5. ListNode(int val) : val(val), next(nullptr) {} — the second constructor. If you make a node and hand it a number, it stores that number in val and still sets next to nullptr.

Notice that in both cases next starts as nullptr. That is on purpose: a brand-new node is not connected to anything yet. We are responsible for linking it to other nodes later, whenever we decide to. The constructor just gives us a clean, standalone node to start with.

Defining a Node in a Singly Linked List
Diagram — click to zoom (scroll / drag to pan)