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 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.

How Insert Works

  1. Search for the insertion point at each level, recording the rightmost node before the target at every level ('update' array).
  2. Flip a coin repeatedly; each 'heads' promotes the new node one level higher (commonly capped at log(n)).
  3. Create the new node at the determined height.
  4. 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

  1. Level 0 predecessor before 15: 12 → insert 15 between 12 and 17
  2. Level 1 predecessor before 15: 9 → insert 15 between 9 and 19
  3. 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

Try your own input
InsertO(log n) exp.
Search-like walk finds the position; a coin flip decides the node height; the value is spliced into every lane up to that height.
H
L3
H325
L2
H31225
L1
H37121925
L0
H36791217192125∅
Active comparison Skip lane Base level
Insert
Press ▶ Run to watch it step by step.
Python · Skip Lists in Code
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 15
Input: Insert 15 into skip list with levels up to 2, coin flips give height 2

Behind 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

TimeO(log n) expected
SpaceO(1) expected extra per insert (amortized across levels)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.