How a skip list stacks random linked lists

Short answer: A skip list is an ordered dictionary built from linked lists stacked in levels. Every key appears in the bottom list. A key is copied into the next level up with probability 1/2, independently, so the expected number of levels is O(log n) and the expected search cost is O(log n). Insert and delete splice the same towers. The structure has the dictionary bounds of a balanced tree and the local edits of a linked list.


What the problem actually asks

A sorted linked list searches in linear time because each step advances one key. Extra forward pointers, of geometrically increasing span, let a search skip ahead and drop down a level when the next pointer would pass the target. Deterministic spans are a balanced tree in disguise. Random promotions produce the same expected span with no rotations.

The coin flips are chosen at insertion and stored as the height of the node’s tower. A search never flips a coin. It only reads pointers.


What the algorithm guarantees

Search, insert, and delete are O(log n) expected time when each node of height at least k is promoted to height at least k+1 with probability 1/2. The expected tower height of a node is O(1), and the maximum height in the list is O(log n) with high probability. Extra memory is expected O(n). A finger search, which starts from a nearby key, is faster and is one reason skip lists appear in concurrent and database indexes.

The bound is expectation and high probability, not a worst-case height. An unlucky run of promotions can build a tall tower, and an unlucky absence of promotions can leave a long bottom-level gap. A red-black tree does not have that coin. A skip list does not rotate.


The working steps

  1. Maintain a header tower taller than any node. Each level of the header points at the first node that reaches that level, or at null.
  2. Search from the top level of the header. While the next pointer on this level is non-null and its key is smaller than the target, advance. Then drop one level. The walk ends at the bottom node that would precede the target.
  3. Insert after that predecessor. Choose a height by flipping a coin until tails, capped at a maximum level. Splice the new tower into each level it reaches, using the predecessors the search recorded.
  4. Delete unsplices the tower from every level it occupies.
  5. Keep the header height at one more than the tallest node so a new maximum still has a place to link.

Expected logarithmic search needs the random promotions. A single sorted list is the bottom level with no tower. Deterministic skip lists exist and use a different promotion rule to guarantee the bound. Concurrent skip lists add locks or atomic pointers and do not change the search geometry.


Approaches that look simpler and fail

Approach Why it fails when every node is promoted with probability 1
Promote every node to the same height Every level is a full copy of the list. The search still walks Θ(n) nodes, now on several levels.
Flip the coin during search The height is a property of the node, chosen at insert. A search that flips again follows a structure that is not there.
Splice only the bottom level on insert The tower’s upper pointers are missing, and later searches skip over a node that should have been a landing point, or fail to find it from above.
Quote worst-case O(log n) for the coin-flip version The height is random. The worst-case walk is linear. The guarantee is expectation or high probability.

A worst-case ordered dictionary is a red-black tree or an AVL tree. A hash table answers membership without order. A skip list is the randomized linked structure with expected logarithmic search and local splices.


A working example

This Python fragment promotes a new skip-list node with probability 1/2, using a seeded coin so the shape is fixed. Search then walks from the top level. It prints yes no for a stored key and a missing key.

import random
rng = random.Random(1)

class N:
    def __init__(self, key, level):
        self.key = key
        self.next = [None] * (level + 1)

head = N(None, 8)

def random_level():
    level = 0
    while level < 8 and rng.random() < 0.5:
        level += 1
    return level

def insert(key):
    level = random_level()
    update = [None] * 9
    node = head
    for i in range(8, -1, -1):
        while node.next[i] and node.next[i].key < key:
            node = node.next[i]
        update[i] = node
    fresh = N(key, level)
    for i in range(level + 1):
        fresh.next[i] = update[i].next[i]
        update[i].next[i] = fresh

def search(key):
    node = head
    for i in range(8, -1, -1):
        while node.next[i] and node.next[i].key < key:
            node = node.next[i]
    node = node.next[0]
    return node is not None and node.key == key

for key in (2, 5, 9):
    insert(key)
assert search(5) and not search(4)
print("yes" if search(5) else "no", "yes" if search(4) else "no")

Where it is used

Redis sorted sets are a skip list plus a hash table, and the LevelDB memtable is a skip list. A lookup skips ahead on the tall lists and falls down to the next one. A balanced tree gives the same dictionary with worse constant factors on those workloads.


When you can drop this algorithm

Drop a skip list when the height must be logarithmic on every run and randomness is unwelcome. Use a red-black tree. Keep it when expected logarithmic dictionaries with simple local updates are enough, including concurrent indexes built on this geometry.


Why this stays published

We publish this so a search for a skip list returns the coin-flip tower and the expected O(log n) search that walks down the levels. A skip list searches an ordered set in expected O(log n) time by stacking linked lists whose nodes are promoted with probability 1/2.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *