Medium interview relevanceRoadmap: Intermediate
TimeInsert or delete at a known node O(1); search O(n)
SpaceO(n)

Why it costs this much

Changing one or two pointers splices a node in or out, and no other node moves.

Code

def insert_after(node, value):
    node.next = Node(value, node.next)    # O(1): rewire two pointers

def find(head, target):
    while head and head.val != target:    # O(n): must walk from the head
        head = head.next
    return head

Common pitfalls

Reaching a position costs O(n). Pointer-chasing is also slow on real hardware, so arrays often win in practice.

Operation by operation

OperationBestAverageWorstSpace
Linked list insert or delete at a known nodeO(1)O(1)O(1)O(1)
Linked list searchO(1)O(n)O(n)O(1)

Linked list insert or delete at a known node. You already hold a pointer to the node (or its predecessor for a singly linked list). Finding the node first is O(n).

Linked list search. Target equally likely to be anywhere. Poor cache locality makes it slower than an array scan of the same size.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list