Design Skiplist
expert · 1205 · lc hard +32 · 59.6% accepted · 724 likes · top 57%
Description
Implement a Skiplist from scratch without using any built-in libraries.
A skiplist is a probabilistic data structure built on layered sorted linked lists that achieves O(log(n)) expected time for add, erase, and search — comparable to balanced BSTs but simpler to code.
Implement the Skiplist class:
- Skiplist() creates an empty skiplist.
- bool search(int target) returns true if target exists in the skiplist, false otherwise.
- void add(int num) inserts num into the skiplist.
- bool erase(int num) removes one occurrence of num and returns true. Returns false if num is absent. If duplicates exist, removing any one is acceptable.
The skiplist must correctly handle duplicate values.
Example 1:
Example 2:
Code