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 ListInserting 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
- Create a new node with the desired value.
- Set tail.next to the new node and newNode.prev to the current tail.
- Update tail to point to the new node; newNode.next stays null.
- 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
- tail(3).next = newNode(4)
- newNode(4).prev = tail(3)
- tail = newNode(4)
Output: null <- 2 <-> 3 <-> 4 -> null
Press ▶ Run to watch it step by step.
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]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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.