Data Structures · Visualization

Doubly LL: Insert at Index

Doubly Linked List

Inserting at a chosen index places a new node between two existing neighbors. Time: O(n) to reach the index, O(1) to relink. Space: O(1).

Big-O: Insert/delete at either end O(1); extra memory for prev pointers.

Doubly LL: Insert at Index

Doubly Linked List

Inserting at a chosen index places a new node between two existing neighbors. You walk to the target position (from whichever end is closer, since the list can be traversed both ways) and then rewire four pointers: the new node's next/prev, and its new neighbors' prev/next.

How Insert at Index Works

  1. Traverse to the node currently at the target index, call it 'current'.
  2. Set newNode.prev to current.prev and newNode.next to current.
  3. Set current.prev.next to newNode (if current.prev exists).
  4. Set current.prev to newNode.

Insert at Index Worked Example

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

  1. current = node at index 1 (value 2)
  2. newNode(99).prev = current.prev (node 1); newNode(99).next = current (node 2)
  3. node1.next = newNode(99)
  4. node2.prev = newNode(99)

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

Try your own input
Doubly LL: Introduction
Like a linked list, but every node keeps a pointer to both the previous and the next node, so you can walk it in either direction.
head
17
prev · next
30
prev · next
43
prev · next
56
prev · next
tail
69
prev · next
Active node next pointer prev pointer
Doubly LL: Introduction
Press ▶ Run to watch it step by step.
Python · Doubly Linked List in Code
def push_front(head, val):
    node = Node(val)
    node.next = head
    if head:
        head.prev = node
    return node            # new head O(1)

def push_back(head, tail, val):
    node = Node(val)
    node.prev = tail
    if tail:
        tail.next = node
    return node            # new tail O(1)
# example: insert 99 at index 1 on [1, 2, 3]
Input: Insert 99 at index 1 into null <- 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
SpaceO(1)

When to Use Insert at Index

  • Inserting a new song into a specific spot in a playlist
  • Keeping a sorted doubly linked list in order
  • Editors that insert content at a cursor position

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.