DEV Community

sylvia uwa
sylvia uwa

Posted on

# The Part About Linked Lists Being O(1) That Confused Me

I spent way too much time being confused by one sentence about linked lists:

“Insertion is O(1) because you only need to change the pointers.”

Okay… but how did I get to the node in the first place? 😂

If I have:

A → B → C → D

and I already have a reference to node C, then yes:

C.next = X
X.next = D
Enter fullscreen mode Exit fullscreen mode

That's O(1).

But what if all I know is:

“Find the node containing C and insert X after it.”

Now I have to traverse the list to find C.

That's O(n).

And this was the part that wasn't clicking for me.

I realized the important distinction is:

Finding the node ≠ inserting the node.

Linked lists don't magically make finding things faster.

They make rearranging the structure cheap once you already have the node/reference.

So when we say linked list insertion is O(1), there's often a hidden assumption:

“Given a reference to the node…”

And honestly, that little phrase changes everything.

I'm starting to realize that a lot of Big-O explanations aren't necessarily wrong they just leave out the context of what you already have.😅

Top comments (0)