Skip List
Imagine a subway system. The local train stops everywhere. But there's also an express train that skips most stops and only hits the major ones. And maybe a super-express that skips even more. You ride the express as far as you can, then switch to local for the last bit.
A skip list works exactly like that. At the bottom is a normal sorted linked list — every element in order. Above it are express lanes: sparser linked lists that skip over many elements, letting you jump farther with each step.
This gives \(O(\log n)\) average search — the same as a balanced BST — without any of the complex rotation logic. The structure is maintained using randomness instead.
← → arrow keys to step · click elements to interact
Skip List: Insert & Search
Building the levels — coin flips
When you insert a new element, it always goes into the bottom level. Then you flip a coin. Heads? Also insert it into level 1. Flip again. Heads again? Also insert into level 2. Keep flipping until tails. On average, half the elements appear in level 1, a quarter in level 2, and so on. This naturally creates the logarithmic structure.
Searching
Start at the top-left (highest level). Move right as long as the next value is less than or equal to your target. When you can't move right, drop down one level. Repeat until you reach the bottom level. You've either found the target or confirmed it's absent.
- Each level halves the remaining candidates on average
- Total levels ≈ \(\log_2(n)\), so total steps ≈ \(O(\log n)\)
Insertion and deletion
For insertion: search to find where the element belongs at each level, then link it in. For deletion: find the element at each level and unlink it. Both mirror the search pattern and take \(O(\log n)\) average.
Why not just use a BST?
Balanced BSTs (AVL, Red-Black) guarantee \(O(\log n)\) worst-case but require complex rotations to stay balanced. Skip lists achieve the same average performance with simpler code. They're also easier to make concurrent — multiple threads can insert/delete without as much locking.