Data Structures · Visualization
Linked List: Insert at End
Linked List
Inserting at the end adds a new node after the current last node, making it the new tail. Time: O(n) without a tail pointer, O(1) with one. Space: O(1).
Big-O: Lookup O(n); insert/delete at head O(1).
Linked List: Insert at End
Linked ListInserting at the end adds a new node after the current last node, making it the new tail. Without a tracked tail pointer, this requires walking the whole list first to find the current last node; with a tail pointer kept up to date, it can be done instantly.
How Insert at End Works
- Create a new node with the desired value; its 'next' will be null.
- If the list is empty, set head to the new node and stop.
- Otherwise, traverse from head until you find the node whose 'next' is null (the current tail).
- Set that node's 'next' to the new node.
Insert at End Worked Example
Input: Insert 4 at end of head -> 2 -> 3 -> null
- Traverse: current = 2 → next is 3, move on
- current = 3 → next is null, this is the tail
- tail.next = newNode(4)
Output: head -> 2 -> 3 -> 4 -> null
Press ▶ Run to watch it step by step.
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 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
- Implementing a queue (enqueue operation) using a linked list
- Appending new events to a log or history in arrival order
- Building a list while preserving the original insertion order
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.