Insert at End Tail Insert in DSA C# - 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 class Node {
public int data;
public Node next;
public Node(int value) { data = value; next = null; }
}
public class LinkedList {
public Node head;
public void InsertAtEnd(int value) {
Node newNode = new Node(value);
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 at the end of a singly linked list by walking through the list until it finds the last node.
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, so roughly n times where n is the number of nodes.
As the list gets longer, the time to find the end grows in a straight line.
| 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 number of steps grows directly with the size of the list.
Time Complexity: O(n)
This means the time to insert at the end grows linearly with the number of nodes in the list.
[X] Wrong: "Inserting at the end is always fast and takes constant time."
[OK] Correct: Without a direct pointer to the last node, we must walk through the whole list, which takes longer as the list grows.
Understanding this helps you explain why adding at the end of a linked list can be slow without extra pointers, showing your grasp of data structure details.
"What if we kept a pointer to the last node (tail)? How would the time complexity change?"
Practice
InsertAtEnd method do in a singly linked list?Solution
Step 1: Understand the method name
The method nameInsertAtEndsuggests adding a node at the end of the list.Step 2: Recall linked list operations
Inserting at the end means adding a new node after the current last node (tail).Final Answer:
Adds a new node at the end (tail) of the list -> Option AQuick Check:
InsertAtEnd = Add node at tail [OK]
- Confusing insert at end with insert at beginning
- Thinking it removes nodes instead of adding
- Mixing up tail and head operations
Solution
Step 1: Create a new node with data
We create a new node usingnew Node(data).Step 2: Link new node at the end
To insert at end, settail.next = newNodeand updatetail = newNode.Final Answer:
Node newNode = new Node(data); tail.next = newNode; tail = newNode; -> Option AQuick Check:
Insert at tail = tail.next = newNode; tail = newNode [OK]
- Assigning newNode.next to head instead of tail.next
- Forgetting to update tail pointer
- Using prev pointer in singly linked list
LinkedList list = new LinkedList(); list.InsertAtEnd(10); list.InsertAtEnd(20); list.InsertAtEnd(30); list.PrintList();
Assuming
PrintList() prints nodes as value -> value -> ... -> null.Solution
Step 1: Insert nodes at end in order
Nodes 10, 20, 30 are inserted at the tail one by one, so list order is 10, then 20, then 30.Step 2: Print list in order
Printing shows nodes in insertion order ending with null.Final Answer:
10 -> 20 -> 30 -> null -> Option BQuick Check:
InsertAtEnd preserves order: 10,20,30 [OK]
- Assuming insert at end reverses order
- Missing last node in print
- Confusing head and tail positions
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
The loop finds the last node wheretemp.next == null.Step 2: Check how new node is linked
Assigningtemp = newNode;only changes local variable, not the last node'snextpointer.Step 3: Correct linking
Should assigntemp.next = newNode;to link new node at end.Final Answer:
The linetemp = newNode;does not link the new node to the list -> Option DQuick Check:
Must set temp.next = newNode, not temp = newNode [OK]
- Assigning newNode to temp instead of temp.next
- Not updating head when list empty
- Incorrect loop condition causing infinite loop
Solution
Step 1: Insert new node at end
Adding 35 at end appends it after 25, so list becomes 5 -> 15 -> 25 -> 35 -> null.Step 2: Handle empty list case
If list is empty, bothheadandtailmust point to the new node to start the list properly.Final Answer:
Sequence: 5 -> 15 -> 25 -> 35 -> null; If empty, set head = newNode and tail = newNode -> Option CQuick Check:
Insert at end appends; empty list needs head and tail set [OK]
- Not updating both head and tail when list is empty
- Inserting new node at head instead of tail
- Only updating tail without linking new node
Ready to prove it?
Pass the test and this pattern moves unknown
