[LeetCode] Design Skiplist

1206. Design Skiplist

Design a Skiplist without using any built-in libraries.

A skiplist is a data structure that takes O(log(n)) time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists is just simple linked lists.

For example, we have a Skiplist containing [30,40,50,60,70,90] and we want to add 80 and 45 into it. The Skiplist works this way:

  • Artyom Kalinin [CC BY-SA 3.0], via Wikimedia Commons

You can see there are many layers in the Skiplist. Each layer is a sorted linked list. With the help of the top layers, add, erase and search can be faster than O(n). It can be proven that the average time complexity for each operation is O(log(n)) and space complexity is O(n).

See more about Skiplist: https://en.wikipedia.org/wiki/Skip_list

Implement the Skiplist class:

  • Skiplist() Initializes the object of the skiplist.
  • bool search(int target) Returns true if the integer target exists in the Skiplist or false otherwise.
  • void add(int num) Inserts the value num into the SkipList.
  • bool erase(int num) Removes the value num from the Skiplist and returns true. If num does not exist in the Skiplist, do nothing and return false. If there exist multiple num values, removing any one of them is fine.

Note that duplicates may exist in the Skiplist, your code needs to handle this situation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
struct SkipNode {
SkipNode* next;
SkipNode* down;
int val;
SkipNode(int v) : val(v), next(nullptr), down(nullptr) {}
SkipNode(int v, SkipNode* next, SkipNode* down) : val(v), next(next), down(down) {}
};

class Skiplist {
SkipNode* head;
public:
Skiplist() : head(new SkipNode(-1)) {}

bool search(int target) {
SkipNode* runner = head;
while(runner) {
while(runner->next and runner->next->val < target) runner = runner->next;
if(runner->next and runner->next->val == target) return true;
runner = runner->down;
}
return false;
}

void add(int num) {
vector<SkipNode*> nodes;
SkipNode* runner = head;
while(runner) {
while(runner->next and runner->next->val < num) runner = runner->next;
nodes.push_back(runner);
runner = runner->down;
}

SkipNode* down = nullptr;

while(!nodes.empty()) {
auto node = nodes.back(); nodes.pop_back();
SkipNode* now = new SkipNode(num, node->next, down);
node->next = now;
down = now;

if(rand() & 1) break;
}
}

bool erase(int num) {
if(!search(num)) return false;
SkipNode* runner = head;
while(runner) {
while(runner->next and runner->next->val < num) runner = runner->next;
if(runner->next and runner->next->val == num) runner->next = runner->next->next;
runner = runner->down;
}
return true;
}
};

/**
* Your Skiplist object will be instantiated and called as such:
* Skiplist* obj = new Skiplist();
* bool param_1 = obj->search(target);
* obj->add(num);
* bool param_3 = obj->erase(num);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/02/PS/LeetCode/design-skiplist/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.