Data Structures · Visualization

Linked List: Insert at Index

Linked List

Inserting at a specific index places a new node between two existing nodes at that position. Time: O(n) to reach the index, O(1) to relink once there. Space: O(1).

Big-O: Lookup O(n); insert/delete at head O(1).

Linked List: Insert at Index

Linked List

Inserting at a specific index places a new node between two existing nodes at that position. You first need to walk the list to find the node just before the target index, then re-point the links so the new node slots in between.

How Insert at Index Works

  1. If the index is 0, insert at the front and stop.
  2. Traverse the list, counting nodes, until you reach the node just before the target index (the 'previous' node).
  3. Point the new node's 'next' to previous.next (the node currently at the target index).
  4. Point previous.next to the new node.

Insert at Index Worked Example

Input: Insert 99 at index 2 into head -> 1 -> 2 -> 3 -> null

  1. Traverse to index 1 (value 2) — this is 'previous'
  2. newNode(99).next = previous.next (node 3)
  3. previous.next = newNode(99)

Output: head -> 1 -> 2 -> 99 -> 3 -> null

Try your own input
Linked List: Introduction
A chain of nodes, each holding a value plus a pointer to the next node — unlike an array, the values are not stored side by side in memory.
12
99
37
5
71
∅
Active / pointers Result null (end) Random pointer
Linked List: Introduction
Press ▶ Run to watch it step by step.
Python · Singly Linked List in Code
def insert_front(head, val):
    node = Node(val)
    node.next = head
    return node            # new head O(1)

def insert_end(head, val):
    node = Node(val)
    if not head:
        return node
    cur = head
    while cur.next:        # walk to tail O(n)
        cur = cur.next
    cur.next = node
    return head
# example: insert 99 at index 2 on [1, 2, 3]
Input: Insert 99 at index 2 into head -> 1 -> 2 -> 3 -> null

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Insert at Index Time & Space Complexity

TimeO(n) to reach the index, O(1) to relink once there
SpaceO(1)

When to Use Insert at Index

  • Inserting a task at a specific priority position in a task list
  • Keeping a sorted linked list in order as new values arrive
  • Editing operations in text editors represented as linked structures

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.