Sub topic 3
Nodes, Forward Pointers and the Head Sentinel
This chapter covers the data structure behind a skip list: the node, its array of forward pointers, and the head sentinel. It assumes you know singly linked lists and pointer traversal, Big-O notation, and basic Python classes. The previous chapter introduced the idea of layered lists. Here you will see how those layers are stored in memory.
From layers to a single node type
A picture of a skip list shows several linked lists stacked on top of each other. The bottom layer holds every key in sorted order. Each higher layer holds a subset of the keys below it.
The code does not store these layers as separate lists. Each key is stored once, in one node. A node that appears in three layers is still one object. It carries one forward pointer for each layer it belongs to.
- A forward pointer is a reference to the next node on a given layer.
- A node's level (or height) is the number of layers it appears in.
- A node of level 3 has three forward pointers, at indexes 0, 1 and 2.
Index 0 is the bottom layer, which links all nodes. Higher indexes link to nodes further ahead.
The node class
class Node:
__slots__ = ("key", "forward")
def __init__(self, key, level):
self.key = key
self.forward = [None] * levelThe forward list has exactly level entries. Each entry starts as None, which means "no next node on this layer". forward[i] is the next node on layer i.
__slots__ is an optional memory optimization. It stops Python from creating a per-object dictionary, which matters when a list holds many nodes.
The head sentinel
A sentinel is a special node that does not hold real data. It exists so that code does not need special cases.
The skip list's head is a sentinel. It has no real key, so its key is None. It is created with MAX_LEVEL forward pointers, so it can start a traversal on any layer that exists.
class SkipList:
MAX_LEVEL = 16
P = 0.5
def __init__(self):
self.head = Node(None, self.MAX_LEVEL)
self.level = 1Two attributes describe the structure:
| Attribute | Meaning |
|---|---|
| `MAX_LEVEL` | The most layers any node may have. The head always has this many pointers. |
| `level` | The number of layers currently in use. It starts at 1 and changes as nodes are added and removed. |
Without a sentinel, inserting before the first real key would mean changing a separate "first node" variable for each layer. With the head, every insertion is the same operation: set a pointer in some predecessor node. The predecessor is sometimes the head.
Why search starts at `level - 1`
The head has 16 pointers, but most are None in a small list. Starting at layer 15 would waste steps. The list tracks level, the highest layer in use, so traversal starts at index level - 1.
The head is never compared against a key. The search loop compares only node.forward[i].key, which belongs to a real node. That is why None as the head's key is safe.
Walking one layer
This helper shows how forward pointers are followed on one layer. It uses the full class from the Topic Spine and prints every key on a chosen layer.
def keys_on_layer(skip_list, i):
result = []
node = skip_list.head.forward[i]
while node is not None:
result.append(node.key)
node = node.forward[i]
return resultLayer 0 returns all keys in sorted order. A higher layer returns a sorted subset. Calling it with i >= skip_list.level normally returns [], because those head pointers are None.
Common errors
- Reading a pointer above a node's level. A node of level 2 has no
forward[2]. Traversal only followsforward[i]from nodes that were reached on layeri, so this is safe. Hand-written code that mixes layers can raiseIndexError. - Giving the head too few pointers. If the head has fewer than
MAX_LEVELpointers, a new node taller than the head cannot be linked in. - Forgetting to update
level. If a new node is taller thanlevel,levelmust grow. Otherwise searches never start high enough to use the new layers.
Exercise
A skip list has MAX_LEVEL = 16 and currently level = 3. A new node is created with level 2. How many forward pointers does the new node have, and how many does the head have?
Show answer
The new node has 2 forward pointers (indexes 0 and 1). The head always has 16, one for each possible layer, regardless of the current level value.