Insert at End Tail Insert in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to add a new item at the end of a list.
How does the time needed change as the list grows bigger?
Analyze the time complexity of the following code snippet.
function insertAtEnd(head, value) {
let newNode = { data: value, next: null };
if (!head) {
return newNode;
}
let current = head;
while (current.next) {
current = current.next;
}
current.next = newNode;
return head;
}
This code adds a new node with the given value at the end of a singly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through each node until the last one.
- How many times: It runs once for each node in the list until the end is reached.
As the list gets longer, the time to find the end grows in a straight line with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach the end |
| 100 | About 100 steps to reach the end |
| 1000 | About 1000 steps to reach the end |
Pattern observation: The steps increase directly with the number of nodes; doubling nodes doubles the steps.
Time Complexity: O(n)
This means the time to insert at the end grows linearly with the list size.
[X] Wrong: "Inserting at the end always takes the same time no matter the list size."
[OK] Correct: Without a direct pointer to the last node, we must walk through all nodes, so bigger lists take more time.
Understanding this helps you explain how linked lists work and why keeping track of the last node can speed up insertions.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the operation
Insert at End means adding a new node after the last node in the linked list.Step 2: Compare with other operations
Other options describe removing or adding at the start, which is different from tail insert.Final Answer:
Adds a new node at the end of the linked list -> Option AQuick Check:
Insert at End = Add node at tail [OK]
- Confusing tail insert with head insert
- Thinking it removes nodes instead of adding
- Assuming it inserts in the middle
Solution
Step 1: Identify correct node creation
Node should have data and next properties; next set to null for tail.Step 2: Insert at end
Set current tail's next to newNode, then update tail to newNode.Final Answer:
let newNode = { data: value, next: null }; tail.next = newNode; tail = newNode; -> Option BQuick Check:
Correct node creation and tail update [OK]
- Using wrong property names like nextNode
- Inserting at head instead of tail
- Not updating tail pointer
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
insertAtEnd(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
}
}
const list = new LinkedList();
list.insertAtEnd(5);
list.insertAtEnd(10);
list.insertAtEnd(15);
let current = list.head;
let result = '';
while(current) {
result += current.data + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);Solution
Step 1: Insert nodes at end
Nodes 5, 10, 15 are inserted sequentially at the tail.Step 2: Traverse and print list
Traversal prints nodes in insertion order: 5 -> 10 -> 15 -> null.Final Answer:
5 -> 10 -> 15 -> null -> Option AQuick Check:
Tail insert preserves order [OK]
- Confusing head and tail order
- Missing last node in output
- Printing reversed list
insertAtEnd(data) {
const newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
} else {
this.tail.next = newNode;
}
// Missing update of tail pointer
}Solution
Step 1: Identify missing tail update
After inserting newNode at tail.next, tail pointer must update to newNode.Step 2: Fix by adding tail update
Add 'this.tail = newNode;' after 'this.tail.next = newNode;' to keep tail correct.Final Answer:
Add 'this.tail = newNode;' after setting tail.next -> Option DQuick Check:
Tail pointer must update after insertion [OK]
- Not updating tail pointer causes broken list
- Changing head instead of tail in else block
- Incorrectly setting newNode.next to tail
Solution
Step 1: Create new node with value 4
Initialize newNode with data=4 and next=null.Step 2: Link newNode after current tail and update tail
Set tail.next = newNode to link, then update tail = newNode to move tail pointer.Final Answer:
Create newNode(4), tail.next = newNode, tail = newNode -> Option CQuick Check:
Tail insert requires linking and tail update [OK]
- Linking new node to head instead of tail
- Setting newNode.next to tail (wrong direction)
- Updating tail before linking new node
Ready to prove it?
Pass the test and this pattern moves unknown
