Practice/Meta/Leetcode 1206. Design Skiplist
CodingOptional
Design and implement a Skip List data structure that supports efficient insertion, deletion, and search operations. A skip list is a probabilistic data structure that allows O(log n) average time complexity for these operations by maintaining multiple levels of linked lists.
In a skip list:
Your implementation should support the following operations:
search(target): Returns true if the target value exists in the skip list, false otherwiseadd(num): Inserts a value into the skip list (duplicates are allowed)remove(num): Removes one occurrence of a value from the skip list if it exists, returns true if removed, false otherwiseSkipList class with methods for search, add, and removeExample 1:
` Operations: skiplist = SkipList() skiplist.add(1) skiplist.add(2) skiplist.add(3) skiplist.search(1) // returns true skiplist.search(4) // returns false
Explanation: The skip list contains {1, 2, 3}. Searching for 1 finds it, but 4 is not present. `
Example 2:
` Operations: skiplist = SkipList() skiplist.add(5) skiplist.add(5) skiplist.remove(5) // returns true skiplist.search(5) // returns true
Explanation: After adding 5 twice, one remove operation leaves one copy of 5 remaining. `
Example 3:
` Operations: skiplist = SkipList() skiplist.remove(10) // returns false skiplist.add(10) skiplist.add(15) skiplist.add(20) skiplist.search(15) // returns true skiplist.remove(20) // returns true skiplist.search(20) // returns false
Explanation: Removing from empty list returns false. After additions, elements can be found and removed. `