The Wayback Machine - https://web.archive.org/web/20241007113156/https://www.geeksforgeeks.org/skip-list/
Open In App

Skip List – Efficient Search, Insert and Delete in Linked List

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis.

In a skip list, elements are organized in layers, with each layer having a smaller number of elements than the one below it. The bottom layer is a regular linked list, while the layers above it contain “skipping” links that allow for fast navigation to elements that are far apart in the bottom layer. The idea behind this is to allow for quick traversal to the desired element, reducing the average number of steps needed to reach it.

Skip lists are implemented using a technique called “coin flipping.” In this technique, a random number is generated for each insertion to determine the number of layers the new element will occupy. This means that, on average, each element will be in log(n) layers, where n is the number of elements in the bottom layer.

Skip lists have an average time complexity of O(log n) for search, insertion and deletion, which is similar to that of balanced trees, such as AVL trees and red-black trees, but with the advantage of simpler implementation and lower overhead.

In summary, skip lists provide a simple and efficient alternative to balanced trees for certain use cases, particularly when the average number of elements in the list is large.

Can we search in a sorted linked list better than O(n) time? The worst-case search time for a sorted linked list is O(n) as we can only linearly traverse the list and cannot skip nodes while searching. For a Balanced Binary Search Tree, we skip almost half of the nodes after one comparison with the root. For a sorted array, we have random access and we can apply Binary Search on arrays. Can we augment sorted linked lists to search faster? The answer is Skip List. The idea is simple, we create multiple layers so that we can skip some nodes. See the following example list with 16 nodes and two layers. The upper layer works as an “express lane” that connects only the main outer stations, and the lower layer works as a “normal lane” that connects every station. Suppose we want to search for 50, we start from the first node of the “express lane” and keep moving on the “express lane” till we find a node whose next is greater than 50. Once we find such a node (30 is the node in the following example) on “express lane”, we move to “normal lane” using a pointer from this node, and linearly search for 50 on “normal lane”. In the following example, we start from 30 on the “normal lane” and with linear search, we find 50. What is the time complexity with two layers? worst-case time complexity is several nodes on the “express lane” plus several nodes in a segment (A segment is several “normal lane” nodes between two “express lane” nodes) of the “normal lane”. So if we have n nodes on “normal lane”, ?n (square root of n) nodes on “express lane” and we equally divide the “normal lane”, then there will be ?n nodes in every segment of “normal lane”. ?n is an optimal division with two layers. With this arrangement, the number of nodes traversed for a search will be O(?n). Therefore, with O(?n) extra space, we can reduce the time complexity to O(?n).

Advantages of Skip List:

  • The skip list is solid and trustworthy.
  • To add a new node to it, it will be inserted extremely quickly. 
  • Easy to implement compared to the hash table and binary search tree
  • The number of nodes in the skip list increases, and the possibility of the worst-case decreases
  • Requires only ?(logn) time in the average case for all operations.
  • Finding a node in the list is relatively straightforward.

Disadvantages of Skip List:

  • It needs a greater amount of memory than the balanced tree.
  • Reverse search is not permitted.
  • Searching is slower than a linked list
  • Skip lists are not cache-friendly because they don’t optimize the locality of reference

Can we do better? The time complexity of skip lists can be reduced further by adding more layers. The time complexity of the search, the insert, and delete can become O(Logn) in an average cases with O(n) extra space. We will soon be publishing more posts on Skip Lists. References MIT Video Lecture on Skip Lists http://en.wikipedia.org/wiki/Skip_list

Sure, here are some related subtopics with details:

  1. Coin flipping technique: The coin flipping technique is used to randomly determine the number of layers an element will occupy in a skip list. When a new element is inserted, a random number is generated and compared to a predetermined threshold. If the random number is less than the threshold, the element is inserted into the next layer. This process is repeated until the random number is greater than the threshold, at which point the element is inserted into the bottom layer. The coin flipping technique is what gives skip lists their probabilistic nature and allows for their efficient average time complexity.
  2. Search operation: The search operation in a skip list involves traversing the layers from the top to bottom, skipping over elements that are not of interest, until the desired element is found or it is determined that it does not exist in the list. The skipping links in each layer allow for quick navigation to the desired element, reducing the average number of steps needed to reach it. The average time complexity of search in a skip list is O(log n), where n is the number of elements in the bottom layer.
  3. Insertion operation: The insertion operation in a skip list involves generating a random number to determine the number of layers the new element will occupy, as described in the coin flipping technique, and then inserting the element into the appropriate layers. The insertion operation preserves the sorted order of the elements in the list. The average time complexity of insertion in a skip list is O(log n), where n is the number of elements in the bottom layer.
  4. Deletion operation: The deletion operation in a skip list involves finding the element to be deleted and then removing it from all layers in which it appears. The deletion operation preserves the sorted order of the elements in the list. The average time complexity of deletion in a skip list is O(log n), where n is the number of elements in the bottom layer.
  5. Complexity analysis: The average time complexity of search, insertion, and deletion in a skip list is O(log n), where n is the number of elements in the bottom layer. This is due to the use of the coin flipping technique, which allows for quick navigation to the desired element, and the probabilistic nature of skip lists, which ensures that the average number of steps needed to reach an element is logarithmic in the number of elements in the bottom layer. However, the worst-case time complexity of these operations can be O(n), which occurs in the rare case where all elements are inserted into only the bottom layer.

Previous Article
Next Article

Similar Reads

When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists. We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, whi
15+ min read
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert a new node at the beginning.A function to travers
10 min read
Implementation of Search, Insert and Delete in Treap
We strongly recommend to refer set 1 as a prerequisite of this post.Treap (A Randomized Binary Search Tree)In this post, implementations of search, insert and delete are discussed.Search: Same as standard BST search. Priority is not considered for search. C/C++ Code // C function to search a given key in a given BST TreapNode* search(TreapNode* roo
15+ min read
Search, Insert, and Delete in an Unsorted Array | Array Operations
In this post, a program to search, insert, and delete operations in an unsorted array is discussed. Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:[GFGTABS] C++ // C++ program to implement linear // search in u
15+ min read
Design a data structure that supports insert, delete, search and getRandom in constant time
Design a data structure that supports the following operations in O(1) time. insert(x): Inserts an item x to the data structure if not already present.remove(x): Removes item x from the data structure if present. search(x): Searches an item x in the data structure.getRandom(): Returns a random element from the current set of elements We can use has
5 min read
Search, Insert, and Delete in an Sorted Array | Array Operations
How to Search in a Sorted Array?In a sorted array, the search operation can be performed by using binary search. Below is the implementation of the above approach: [GFGTABS] C++ // C++ program to implement binary search in sorted array #include <bits/stdc++.h> using namespace std; int binarySearch(int arr[], int low, int high, int key) { if (
15+ min read
Insert a linked list into another linked list
Given two linked lists, head1 and head2 of sizes m and n respectively. The task is to remove head1's nodes from the ath node to the bth node and insert head2 in their place. Examples: Input: a = 3, b = 4head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15, head2: 100 -> 101 -> 102 -> 103Output: 10 -> 11 -> 12 -> 100 -> 101 -
10 min read
Sublist Search (Search a linked list in another list)
Given two linked lists, the task is to check whether the first list is present in 2nd list or not. Examples: Input: list1 = 10->20 list2 = 5->10->20 Output : LIST FOUNDInput: list1 = 1->2->3->4 list2 = 1->2->1->2->3->4 Output: LIST FOUNDInput: list1 = 1->2->3->4 list2 = 1->2->2->1->2->3 Outpu
15+ min read
Skip List | Set 3 (Searching and Deletion)
In previous article Skip List | Set 2 (Insertion) we discussed the structure of skip nodes and how to insert an element in the skip list. In this article we will discuss how to search and delete an element from skip list. Searching an element in Skip list Searching an element is very similar to approach for searching a spot for inserting an element
15+ min read
Memory efficient doubly linked list
We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the linked list and the getList() function returns the l
9 min read
Find the closest element in Binary Search Tree | Space Efficient Method
Given a binary search tree and a target node K. The task is to find the node with the minimum absolute difference with given target value K. NOTE: The approach used should have constant extra space consumed O(1). No recursion or stack/queue like containers should be used. Examples: Input: k = 4 Output: 4 Input: k = 18 Output: 17 A simple solution m
10 min read
Efficient search in an array where difference between adjacent is 1
Given an array of n integers. Each array element is obtained by adding either +1 or -1 to previous element i.e absolute difference between any two consecutive elements is 1. The task is to search an element index with the minimum number of comparison (less than simple element by element search). If the element is present multiple time, then print t
5 min read
Why ternary search is not efficient?
When it comes to searching algorithms, ternary search is a technique that divides the search space into three parts instead of two like in binary search. Below are the reasons why ternary search is not considered efficient in certain scenarios. Comparison with Binary SearchIn binary search, the search space is halved with each comparison, making it
2 min read
Skip List | Set 2 (Insertion)
We have already discussed the idea of Skip list and how they work in Skip List | Set 1 (Introduction). In this article, we will be discussing how to insert an element in Skip list. Deciding nodes level Each element in the list is represented by a node, the level of the node is chosen randomly while insertion in the list. Level does not depend on th
15+ min read
Queries to insert, delete one occurrence of a number and print the least and most frequent element
Given Q queries of type 1, 2, 3 and 4 as described below. Type-1: Insert a number to the list.Type-2: Delete only one occurrence of a number if exists.Type-3: Print the least frequent element, if multiple elements exist then print the greatest among them.Type-4: Print the most frequent element, if multiple elements exist then print the smallest amo
14 min read
Insert and delete element from spirally sorted matrix
Given a Spirally Sorted 2D matrix mat[][] of size NXN, remove an element R and add another element A with only one traversal and without disrupting the sorted order. In other words, the matrix should remain in a spirally sorted form after both removal and addition operations. Note: R will always be present in the Matrix to remove Examples: Input: m
15+ min read
Efficiently design Insert, Delete and Median queries on a set
Given an empty set initially and a number of queries on it, each possibly of the following types: Insert - Insert a new element ‘x’.Delete - Delete an existing element ‘x’.Median - Print the median element of the numbers currently in the set Example: Input : Insert 1 Insert 4 Insert 7 Median Output : The first three queries should insert 1, 4 and 7
15 min read
Minimum time to write characters using insert, delete and copy operation
We need to write N same characters on a screen and each time we can insert a character, delete the last character and copy and paste all written characters i.e. after copy operation count of total written character will become twice. Now we are given time for insertion, deletion and copying. We need to output minimum time to write N characters on t
8 min read
Implement the insert and delete functions on Priority queue without Array
A priority Queue is a type of queue in which every element is associated with a priority and is served according to its priority. We will use two popular data structures for implementing priority queues without arrays - Fibonacci HeapBinomial HeapFibonacci Heap:Fibonacci heap is a heap data structure that is composed of a collection of min-heap-ord
15+ min read
Minimum cost to convert one given string to another using swap, insert or delete operations
Given two strings A and B of length N and M respectively, the task is to find the minimum cost to convert string A to B using the following operations: A character of string A can be swapped from another character of the same string. Cost = 0.A character can be deleted from string B or can be inserted in the string A. Cost = 1. Examples: Input: A =
6 min read
Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
Design a Data Structure that can support the following operations in O(1) Time Complexity. insert(x): Inserts x in the data structure. Returns True if x was not present and False if it was already present.remove(x): Removes x from the data structure, if present.getRandom(): Returns any value present in the stream randomly. The probability of each e
9 min read
Sorted insert in a doubly linked list with head and tail pointers
A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes. The task is to create a doubly linked list by inserting nodes such that list remains in ascending order on printing from left to ri
10 min read
2-3 Trees | (Search, Insert and Deletion)
In binary search trees we have seen the average-case time for operations like search/insert/delete is O(log N) and the worst-case time is O(N) where N is the number of nodes in the tree. Like other Trees include AVL trees, Red Black Tree, B tree, 2-3 Tree is also a height balanced tree. The time complexity of search/insert/delete is O(log N) . A 2-
4 min read
Trie Data Structure | Insert and Search
The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is commonly used for efficient retrieval and storage of keys in a large dataset. The structure supports operations such as insertion, search, and deletion of keys, making it a valuable tool in fields like computer science and information retrieval. I
15+ min read
Insert a whole linked list into other at k-th position
Given two linked lists and a number k. Insert second linked list in to first at k-th position Examples: Input : a : 1->2->3->4->5->NULL b : 7->8->9->10->11->NULL k = 2 Output :1->2->7->8->9->10->11->3->4->5->NULL Input : a: 10->15->20->NULL b: 11->17->16->18->NULL k =
10 min read
How to insert a Node in a Singly Linked List at a given Position using Recursion
Given a singly linked list as list, a position, and a node, the task is to insert that element in the given linked list at a given position using recursion. Examples: Input: list = 1->2->3->4->5->6->7, node = (val=100,next=null), position = 4 Output: 1->2->3->100->4->5->6->7Explanation: Here the node with valu
14 min read
Given a linked list which is sorted, how will you insert in sorted way
Given a sorted linked list and a value to insert, write a function to insert the value in a sorted way.Initial Linked List Linked List after insertion of 9 Recommended PracticeInsert in a Sorted ListTry It! Algorithm: Let input linked list is sorted in increasing order. 1) If Linked list is empty then make the node as head and return it. 2) If the
14 min read
XOR Linked List - Insert an element at a specific position
Given a XOR linked list and two integers position and value, the task is to insert a node containing value as the positionth node of the XOR Linked List. Examples: Input: 4<-->7<-->9<-->7, position = 3, value = 6 Output: 4<-->7<-->6<-->9<-->7Explanation: Inserting a node at the 3rd position with value 6 mod
15+ min read
Insert a Node at Front/Beginning of a Linked List
Given a linked list, the task is to insert a new node at the beginning/start/front of the linked list. Example: Input: LinkedList = 2->3->4->5, NewNode = 1Output: 1 2 3 4 5 Input: LinkedList = 2->10, NewNode = 1Output: 1 2 10 Approach: To insert a new node at the front, we create a new node and point its next reference to the current he
8 min read
Article Tags :
Practice Tags :