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 List

Inserting 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

  1. Create a new node with the desired value; its 'next' will be null.
  2. If the list is empty, set head to the new node and stop.
  3. Otherwise, traverse from head until you find the node whose 'next' is null (the current tail).
  4. Set that node's 'next' to the new node.

Insert at End Worked Example

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

  1. Traverse: current = 2 → next is 3, move on
  2. current = 3 → next is null, this is the tail
  3. tail.next = newNode(4)

Output: head -> 2 -> 3 -> 4 -> 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 at end 4 on [2, 3]
Input: Insert 4 at end of head -> 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(n) without a tail pointer, O(1) with one
SpaceO(1)

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

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.