1function search(head, maxLevel, target) {
2 let cur = head;
3 for (let i = maxLevel - 1; i >= 0; i--)
4 while (cur.forward[i] && cur.forward[i].value < target)
5 cur = cur.forward[i]; // 同层右移
6 // i-- 即下降一层
7 return cur.forward[0] && cur.forward[0].value === target;
8}
9function insert(head, maxLevel, val) {
10 const update = searchPredecessors(head, maxLevel, val);
11 const lvl = randomLevel(); // 几何分布:约 1/2 晋升
12 const node = { value: val, forward: [] };
13 for (let i = 0; i < lvl; i++) {
14 node.forward[i] = update[i].forward[i];
15 update[i].forward[i] = node; // 各层插入
16 }
17}