Skip list

On this page 7

A skip list is a probabilistic ordered data structure built from several layers of sorted linked lists, in which higher layers act as express lanes that skip over many elements of the layers below. Among data structures in programming, it offers expected O(log n) search, insertion and deletion while relying on random coin flips instead of the rotations and recoloring used by balanced trees.

William Pugh introduced the skip list in 1989 as a simpler alternative to balanced search trees. It keeps keys in sorted order, supports range queries and ordered iteration, and is easy to make concurrent without global locks, which is why it appears in in-memory databases, key-value stores and standard libraries.

How a skip list is organized

The bottom level, often called level 0, is an ordinary sorted linked list containing every element. Each node is additionally promoted to level 1 with probability p, usually 1/2 or 1/4, then to level 2 with the same probability again, and so on. A node that reaches level k therefore holds k + 1 forward pointers, one per level it belongs to.

A head sentinel spans all levels and marks the start of every list, and many implementations also use a tail sentinel or nil pointer at the end. Because promotion is random, the expected number of nodes on level k is about n times p to the power k, so the number of levels grows like log base 1/p of n.

The parameters below determine how a skip list behaves in memory and speed.

ParameterTypical valueWhat it affects
Promotion probability p1/2 or 1/4Height, pointer count, search steps
Maximum levelAbout log base 1/p of expected nCaps node height and head size
Expected pointers per node2 for p = 1/2, about 1.33 for p = 1/4Memory overhead
Expected search costProportional to log n divided by pLookup latency
Random number sourceFast generator or bit tricksInsertion speed, predictability
Key parameters of a skip list

Search, insertion and deletion in a skip list

Search starts at the top level of the head node and moves right while the next key is smaller than the target. When the next key would overshoot, the search drops one level and continues. At level 0 the next node either holds the target or proves that it is absent. On average, the search makes only a constant number of steps per level, which yields logarithmic cost.

Insertion performs the same descent while recording, at every level, the last node visited before dropping down; this array is often called the update vector. A random height is then drawn for the new node, and its pointers are spliced in after the recorded predecessors on each level up to that height. Deletion uses the same update vector to unlink the node from every level that contains it.

No restructuring of other nodes is ever needed, which is the core advantage: each operation changes only the pointers adjacent to one node. The table below lists the complexity of the main operations.

OperationExpected costWorst-case cost
SearchO(log n)O(n)
InsertionO(log n)O(n)
DeletionO(log n)O(n)
Range scan of k itemsO(log n + k)O(n)
SpaceO(n)O(n log n)
Time and space complexity of skip list operations

The worst case arises only with astronomically unlikely coin sequences; the probability that an operation takes much longer than its expected time falls off exponentially, so practical performance is highly predictable for large n.

Skip lists compared with balanced trees and hash tables

A skip list competes with balanced binary search trees such as AVL trees and red–black trees, because all three maintain sorted order with logarithmic operations. A hash table is faster for exact-key lookups but cannot return keys in order or answer range queries efficiently.

StructureOrdered accessLookup costBalancing method
Skip listYes, with range scansExpected O(log n)Randomized node heights
AVL treeYesWorst-case O(log n)Rotations on height difference
Red–black treeYesWorst-case O(log n)Rotations and recoloring
B-treeYesO(log n), few disk readsNode splits and merges
Hash tableNoExpected O(1)Resizing and rehashing
Skip list compared with related data structures

The key trade-off is simplicity versus guarantees. Balanced trees give deterministic worst-case bounds and usually better cache locality, while skip lists are shorter to implement correctly and much easier to update concurrently, since an insertion touches only local pointers rather than rebalancing a path to the root.

Concurrent skip lists and real-world implementations

Skip lists are a favorite basis for lock-free and fine-grained locking ordered maps. Because insertion links the new node level by level from the bottom up, a node becomes visible at level 0 first, and readers never see an inconsistent tree shape. Using compare-and-swap on forward pointers, multiple threads can insert and delete at once with no global lock.

Well-known uses include the sorted set in Redis, which combines a skip list with a hash table, the memtable in LevelDB and RocksDB, which buffers writes before they are flushed to disk, and ConcurrentSkipListMap in the Java standard library. Apache Lucene also uses skip data inside posting lists to jump quickly through document IDs.

SituationRecommended choiceReason
Concurrent ordered map in memorySkip listLocal updates, lock-free designs
Strict real-time latency boundsBalanced treeDeterministic worst case
Exact-key lookups onlyHash tableConstant expected time
Large on-disk indexB-tree or B+ treeFewer block reads
Write buffer in an LSM storeSkip listFast sorted inserts, ordered flush
Choosing a skip list in practice

Limitations and common implementation mistakes

Skip lists are simple, but several errors recur in practice and some limitations are inherent to the design:

  • Poor cache locality: nodes are scattered in memory, so pointer chasing can make a skip list slower than an array-based B-tree for read-heavy workloads.
  • Weak random generator: a predictable or poorly seeded generator lets an attacker force tall or flat structures and degrade performance toward linear time.
  • Fixed maximum level too low: capping height for a small n and then growing to millions of keys produces long horizontal scans.
  • Memory overhead: storing several forward pointers per node costs more than a plain linked list, especially with p = 1/2.
  • Incorrect concurrent deletion: unlinking upper levels before marking the node logically deleted can cause lost updates in lock-free versions.

In everyday language

Think of a long subway line with a local train that stops at every station and an express train that stops only at some. To reach a distant station, you ride the express as far as possible, then switch to the local for the last few stops.

A skip list works the same way with sorted data. Upper layers skip over many items, lower layers visit every item, and a coin flip decides which items become express stops. The result is that finding something in a list of millions takes only a few dozen steps instead of millions.

Seen in the real world

An online game keeps a live leaderboard of two million players ranked by score. The backend stores the scores in a sorted set whose ordered part is a skip list, paired with a hash table that maps each player to the current score.

When a player finishes a match, the server removes the old entry and inserts the new score, each in roughly twenty to thirty pointer steps. Requests such as “show the top 100” or “show ten players above and below me” become range scans along the bottom level, starting from a node found in logarithmic time. The result is that thousands of score updates per second run alongside constant leaderboard reads, without locking the whole ranking.