Data Structures · Visualization
Skip List: Insert
Skip List
Inserting into a skip list places the new value in sorted position at level 0, then randomly decides (by repeated coin flips) how many additional levels the node should also appear in, splicing it into each chosen level. Time: O(log n) expected. Space: O(1) expected extra per insert (amortized across levels).
Big-O: Search / insert / delete O(log n) expected thanks to the extra skip lanes.
Skip List: Insert
Skip ListInserting into a skip list places the new value in sorted position at level 0, then randomly decides (by repeated coin flips) how many additional levels the node should also appear in, splicing it into each chosen level.
How Insert Works
- Search for the insertion point at each level, recording the rightmost node before the target at every level ('update' array).
- Flip a coin repeatedly; each 'heads' promotes the new node one level higher (commonly capped at log(n)).
- Create the new node at the determined height.
- At each level up to that height, splice the node in right after the recorded predecessor.
Insert Worked Example
Input: Insert 15 into skip list with levels up to 2, coin flips give height 2
- Level 0 predecessor before 15: 12 → insert 15 between 12 and 17
- Level 1 predecessor before 15: 9 → insert 15 between 9 and 19
- Node 15 now appears at levels 0 and 1
Output: 15 is searchable via the fast lane at level 1 as well as level 0
Press ▶ Run to watch it step by step.
def insert(head, max_h, key):
update = [None] * max_h
cur = head
for level in range(max_h - 1, -1, -1): # top → bottom
while (cur.next[level] and
cur.next[level].key < key):
cur = cur.next[level]
update[level] = cur # remember spot
height = random_height(max_h)
node = Node(key, height)
for level in range(height):
node.next[level] = update[level].next[level]
update[level].next[level] = node
return head
# example: insert 15Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Insert Time & Space Complexity
When to Use Insert
- Maintaining a dynamic sorted leaderboard
- In-memory database indexes
- Priority-ordered event queues
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.