Insert at End Tail Insert in DSA Java - Time & Space Complexity
We want to understand how long it takes to add a new item at the end of a linked list.
How does the time needed change as the list grows bigger?
Analyze the time complexity of the following code snippet.
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
This code adds a new node with given data 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 found.
As the list gets longer, the time to find the end grows linearly.
| 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.
Time Complexity: O(n)
This means the time to insert at the end grows in direct proportion to the list size.
[X] Wrong: "Inserting at the end always takes the same time no matter the list size."
[OK] Correct: Because we must walk through all nodes to find the end, so bigger lists take longer.
Knowing this helps you explain why adding at the end of a simple linked list can be slow and when to use a tail pointer for faster inserts.
"What if we kept a pointer to the last node (tail)? 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 of the list.Step 2: Identify the effect on the list
The new node becomes the new tail, extending the list by one node.Final Answer:
Adds a new node at the end (tail) of the list -> Option AQuick Check:
Insert at End = Add node at tail [OK]
- Confusing insert at end with insert at beginning
- Thinking it deletes nodes
- Assuming it searches instead of inserts
Solution
Step 1: Recall Java object creation syntax
In Java, new objects are created using the 'new' keyword followed by the constructor.Step 2: Match syntax for node creation
Correct syntax is: Node newNode = new Node(10); assuming Node class has a constructor taking value.Final Answer:
Node newNode = new Node(10); -> Option AQuick Check:
Use 'new' keyword and constructor for object creation [OK]
- Omitting 'new' keyword
- Calling constructor without 'new'
- Not assigning to a variable
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class Test {
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
Node tail = head.next;
Node newNode = new Node(3);
tail.next = newNode;
tail = newNode;
printList(head);
}
static void printList(Node node) {
while(node != null) {
System.out.print(node.data + " -> ");
node = node.next;
}
System.out.print("null");
}
}Solution
Step 1: Trace initial list creation
head points to node 1, head.next points to node 2, tail points to node 2.Step 2: Insert new node at end and update tail
New node with data 3 is created, tail.next points to newNode, tail updated to newNode.Step 3: Print list
List nodes in order: 1 -> 2 -> 3 -> null.Final Answer:
1 -> 2 -> 3 -> null -> Option CQuick Check:
Tail insert adds node 3 at end [OK]
- Not updating tail after insertion
- Confusing node order
- Printing incomplete list
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp = newNode;
}
}Solution
Step 1: Analyze the while loop and insertion
The loop finds the last node (temp.next == null). After loop, temp points to last node.Step 2: Check how new node is linked
Assigning 'temp = newNode;' only changes temp variable, not the last node's next pointer.Step 3: Correct linking
Should assign 'temp.next = newNode;' to link new node at end.Final Answer:
The line 'temp = newNode;' does not link the new node to the list -> Option BQuick Check:
Link new node by setting lastNode.next = newNode [OK]
- Assigning newNode to temp instead of temp.next
- Not updating head when list is empty
- Wrong loop condition
Solution
Step 1: Create new node with value 20
Use constructor to create new node with data 20 and next as null.Step 2: Traverse to last node
Start from head, move through nodes until node.next is null (node with 15).Step 3: Link new node at end
Set last node's next pointer to new node, extending list to 5 -> 10 -> 15 -> 20 -> null.Final Answer:
Create new node with 20, traverse to last node, set last node's next to new node -> Option DQuick Check:
Insert at end = link new node after last node [OK]
- Setting head to new node incorrectly
- Not linking new node to list
- Inserting at beginning instead of end
Ready to prove it?
Pass the test and this pattern moves unknown
