{"id":125,"date":"2026-11-13T12:32:03","date_gmt":"2026-11-13T04:32:03","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-skip-list-stacks-random-linked-lists\/"},"modified":"2026-11-13T21:50:52","modified_gmt":"2026-11-13T13:50:52","slug":"how-a-skip-list-stacks-random-linked-lists","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-skip-list-stacks-random-linked-lists\/","title":{"rendered":"How a skip list stacks random linked lists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>skip list<\/strong> 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 <strong>1\/2<\/strong>, independently, so the expected number of levels is <strong>O(log n)<\/strong> and the expected search cost is <strong>O(log n)<\/strong>. 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The coin flips are chosen at insertion and stored as the height of the node&#8217;s tower. A search never flips a coin. It only reads pointers.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>Delete unsplices the tower from every level it occupies.<\/li>\n<li>Keep the header height at one more than the tallest node so a new maximum still has a place to link.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when every node is promoted with probability 1<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Promote every node to the same height<\/td>\n<td>Every level is a full copy of the list. The search still walks \u0398(n) nodes, now on several levels.<\/td>\n<\/tr>\n<tr>\n<td>Flip the coin during search<\/td>\n<td>The height is a property of the node, chosen at insert. A search that flips again follows a structure that is not there.<\/td>\n<\/tr>\n<tr>\n<td>Splice only the bottom level on insert<\/td>\n<td>The tower&#8217;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.<\/td>\n<\/tr>\n<tr>\n<td>Quote worst-case O(log n) for the coin-flip version<\/td>\n<td>The height is random. The worst-case walk is linear. The guarantee is expectation or high probability.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>yes no<\/code> for a stored key and a missing key.<\/p>\n\n<pre><code class=\"language-python\">import random\nrng = random.Random(1)\n\nclass N:\n    def __init__(self, key, level):\n        self.key = key\n        self.next = [None] * (level + 1)\n\nhead = N(None, 8)\n\ndef random_level():\n    level = 0\n    while level &lt; 8 and rng.random() &lt; 0.5:\n        level += 1\n    return level\n\ndef insert(key):\n    level = random_level()\n    update = [None] * 9\n    node = head\n    for i in range(8, -1, -1):\n        while node.next[i] and node.next[i].key &lt; key:\n            node = node.next[i]\n        update[i] = node\n    fresh = N(key, level)\n    for i in range(level + 1):\n        fresh.next[i] = update[i].next[i]\n        update[i].next[i] = fresh\n\ndef search(key):\n    node = head\n    for i in range(8, -1, -1):\n        while node.next[i] and node.next[i].key &lt; key:\n            node = node.next[i]\n    node = node.next[0]\n    return node is not None and node.key == key\n\nfor key in (2, 5, 9):\n    insert(key)\nassert search(5) and not search(4)\nprint(&quot;yes&quot; if search(5) else &quot;no&quot;, &quot;yes&quot; if search(4) else &quot;no&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>Redis sorted sets<\/strong> are a skip list plus a hash table, and the <strong>LevelDB<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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. <strong>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.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-trees"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions\/524"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}