Data Structures · Visualization

Doubly LL: Insert at End

Doubly Linked List

Inserting at the end adds a new node after the current tail. Time: O(1) with a tail pointer, O(n) without one. Space: O(1).

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

Doubly LL: Insert at End

Doubly Linked List

Inserting at the end adds a new node after the current tail. With a tail pointer kept up to date, this is instant: connect the new node to the old tail in both directions, then move the tail pointer forward.

How Insert at End Works

  1. Create a new node with the desired value.
  2. Set tail.next to the new node and newNode.prev to the current tail.
  3. Update tail to point to the new node; newNode.next stays null.
  4. If the list was empty, set head to the new node too.

Insert at End Worked Example

Input: Insert 4 at end of null <- 2 <-> 3 -> null

  1. tail(3).next = newNode(4)
  2. newNode(4).prev = tail(3)
  3. tail = newNode(4)

Output: null <- 2 <-> 3 <-> 4 -> 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 at end 4 on [2, 3]
Input: Insert 4 at end of null <- 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 End Time & Space Complexity

TimeO(1) with a tail pointer, O(n) without one
SpaceO(1)

When to Use Insert at End

  • Appending events to a history log in order
  • Implementing 'push back' in a deque
  • Building a playlist where songs are added at the end

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.