Sub topic 1
Why a Sorted Linked List Is Slow to Search
Prerequisites
This chapter assumes you know the following:
- How a singly linked list stores nodes and how to traverse it by following
nextpointers. - Big-O notation, enough to read O(1), O(log n) and O(n).
- Basic Python syntax: classes, loops and
None.
Probability is not needed yet. It appears in later chapters, when node heights are chosen by coin flips.
What a sorted linked list gives you
A sorted linked list keeps its keys in ascending order. Each node holds a key and a pointer to the next node. A skip list is built on top of this structure, so you need to know where the structure is weak.
Here is a minimal version in Python:
class ListNode:
def __init__(self, key, next=None):
self.key = key
self.next = next
class SortedList:
def __init__(self):
self.head = None
self.comparisons = 0
def insert(self, key):
if self.head is None or key < self.head.key:
self.head = ListNode(key, self.head)
return
node = self.head
while node.next is not None and node.next.key < key:
node = node.next
node.next = ListNode(key, node.next)
def contains(self, key):
node = self.head
while node is not None:
self.comparisons += 1
if node.key == key:
return True
if node.key > key:
return False
node = node.next
return False
lst = SortedList()
for k in range(1, 1001):
lst.insert(k)
lst.contains(1000)
print(lst.comparisons) # 1000The search for 1000 visits every node, so it makes 1000 comparisons. The list is sorted, but that does not help: the code can only move one node at a time.
Why sorting does not speed up the search
An array can use binary search because it supports random access. You can jump to index n // 2 in O(1), compare, and discard half of the data.
A linked list has no such jump. To reach the middle node you must follow n / 2 pointers. Binary search on a linked list therefore still costs O(n) pointer hops, even though it makes fewer comparisons.
The only thing sortedness buys you is an early exit. If you are looking for 5 and you reach a node with key 7, the key is not in the list, and you can stop. This helps on misses but does not change the worst case.
| Operation | Sorted array | Sorted linked list |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert, once the position is known | O(n) (shift elements) | O(1) (relink pointers) |
| Insert, including finding the position | O(n) | O(n) |
| Access by index | O(1) | O(n) |
The linked list is cheap to modify but expensive to search. The array is the reverse. An ordered collection that needs fast search and fast updates must find a way to get the benefits of both.
Seeing the cost
The diagram shows a search for 40 in a list of eight keys. Each arrow is one step.
With n keys, a search for the last key follows n - 1 pointers. If the list doubles in size, the cost doubles too.
Common mistakes
- Assuming sorted means fast. Order helps only if you can jump. Without random access, the order is used for early exit and nothing more.
- Applying binary search to a linked list. Finding the midpoint costs O(n) on its own. Repeating this at every step gives about n/2 + n/4 + ... hops, which is still O(n).
- Forgetting the cost of finding the insert position. Insertion is O(1) only after you hold a pointer to the predecessor node. Locating it takes O(n).
The idea that comes next
The problem is that every search starts at the head and walks one node at a time. A shortcut would let the search skip ahead and fall back to single steps only near the target. The next chapter adds extra lists on top of this one so that some nodes can be skipped. Those extra lists are the express lanes that give the skip list its name.
Exercise
A sorted linked list holds 1,000,000 keys. About how many pointer hops does a search for the largest key take, and why can't binary search reduce this number?
Show answer
About 999,999 hops, since the search starts at the head and moves one node at a time. Binary search needs to jump to the middle element, but a linked list has no random access, so reaching the middle takes n / 2 hops. Total work across all the steps stays O(n).
Exercise
Why is inserting into a sorted linked list described as O(1) in one context and O(n) in another?
Show answer
Relinking pointers takes O(1) once you hold the predecessor node. Finding the predecessor requires walking the list from the head, which takes O(n). A full insert by key therefore costs O(n).