Course contentsShow
← Computer Science
Lesson 328 of 2,8728. Linked Data StructuresFree lesson

Inserting at the Head

Adding a new node at the front by updating pointers; O(1) time complexity operation.

Inserting at the Head

What you'll learn: You'll learn how to add a new node at the very beginning of a linked list in constant time.


The Core Idea

When you insert a node at the head (front) of a linked list, you're making it the new first element. This operation is surprisingly simple and efficient.

Here's how it works:

  1. Create a new node with your data
  2. Point the new node's "next" pointer to the current head
  3. Update the head pointer to point to your new node

That's it! The new node is now at the front, and the old first node is now second.

Why This Is Fast

Unlike arrays—where inserting at the front requires shifting all elements—linked lists let you insert at the head without touching any existing nodes. You only modify two pointers:

  • The new node's next pointer
  • The head pointer itself

This takes the same amount of time regardless of whether your list has 10 nodes or 10 million nodes.

The O(1) Guarantee

Because you're performing a fixed number of operations (create node, set two pointers) that don't depend on the list's size, inserting at the head is an O(1) operation—constant time complexity.

Think of it like placing a new card on top of a deck: it doesn't matter how many cards are already there, adding one to the top always takes the same effort.


Key Takeaway: Inserting at the head of a linked list requires only creating a new node and updating two pointers, making it an O(1) constant-time operation regardless of list size.