Nodes and Pointers
Building Blocks
A linked list is just a chain of nodes connected by pointers. Understanding how to create individual nodes and link them together forms the foundation for all linked list operations. Each node is a small container holding two things: your data and directions to the next container.
The Node Structure
A node is a simple structure with two fields. The first field stores the actual data - a number, a string, an object, whatever you're storing in the list. The second field stores a pointer (or reference) to the next node.
In Python, JavaScript, Java, and C#, we implement nodes as classes. In C++ and Go, we use structs. In Rust, we use a struct with special ownership handling. The concept stays the same across languages: bundle data with a link to the next node.
Creating a Single Node
1class Node:
2 def __init__(self, data):
3 self.data = data
4 self.next = None
5
6# Create a node with data = 5
7node = Node(5)
8print(node.data) # 5
9print(node.next) # None
The __init__ method creates a new node. By default, next is None since the node doesn't point anywhere yet.
Linking Two Nodes
Creating individual nodes is simple. Connecting them requires assigning one node's next pointer to point to another node. The order matters - make sure you have a reference to both nodes before linking them.
1# Create two nodes
2first = Node(10)
3second = Node(20)
4
5# Link first to second
6first.next = second
7
8# Now: first -> second -> None
9print(first.data) # 10
10print(first.next.data) # 20
11print(second.next) # None
Assignment first.next = second makes first point to second.
Building a Chain
You can extend this pattern to create longer chains. Each node points to the next, forming a sequence. Keep a reference to the first node (the head) so you can access the entire chain.
1# Create nodes
2head = Node(1)
3second = Node(2)
4third = Node(3)
5fourth = Node(4)
6
7# Link them
8head.next = second
9second.next = third
10third.next = fourth
11
12# Chain: 1 -> 2 -> 3 -> 4 -> None
Build the chain by connecting each node to the next one.
The Null Pointer Marks the End
The last node in the chain must have its next pointer set to null. This tells any code traversing the list where to stop. Without a null terminator, your code would try to follow a pointer to nowhere, causing errors or crashes.
Every programming language has a way to represent "nothing" or "no value": null in Java, JavaScript, C++, and C#, None in Python and Rust, nil in Ruby and Go. When you create a new node, the next pointer defaults to this null value, marking it as the end of the list until you link it to something else.
Pointer Manipulation Fundamentals
All linked list operations boil down to manipulating these pointers. Want to insert a node? Change pointers to include it in the chain. Want to delete a node? Change pointers to skip over it. Want to reverse the list? Flip all the pointers to point backward instead of forward.
The key skill is tracking which pointers need to change and in what order. Change them in the wrong sequence and you might lose access to part of the list. Always save references before breaking connections.
Practice: Building a List from an Array
A common task is converting an array of values into a linked list. You create nodes for each value and link them together in sequence. This requires creating nodes one by one and maintaining the connections.
The next article covers traversing a linked list - walking through the chain from head to end, which is essential for almost every linked list operation.
Try It Yourself
Implement a function that takes an array of integers and builds a linked list from those values. Return the head of the linked list. If the array is empty, return null.