Enqueue Using Linked List in DSA C# - Time & Space Complexity
We want to understand how the time to add an item to a queue using a linked list changes as the queue grows.
How does the number of steps grow when we add more items?
Analyze the time complexity of the following code snippet.
public class Node {
public int Data;
public Node Next;
public Node(int data) { Data = data; Next = null; }
}
public class Queue {
private Node front, rear;
public void Enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = rear = newNode;
return;
}
rear.Next = newNode;
rear = newNode;
}
}
This code adds a new item to the end of a queue implemented with a linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding a new node at the end by updating pointers.
- How many times: This happens once per enqueue call, no loops or recursion inside enqueue.
Each enqueue operation takes the same number of steps regardless of queue size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 pointer updates |
| 100 | About 3 pointer updates |
| 1000 | About 3 pointer updates |
Pattern observation: The work stays the same no matter how big the queue is.
Time Complexity: O(1)
This means adding an item takes a constant amount of time, no matter how many items are already in the queue.
[X] Wrong: "Enqueue takes longer as the queue grows because we have to find the end each time."
[OK] Correct: Because we keep a pointer to the last node (rear), we add directly without searching, so time stays constant.
Knowing that enqueue in a linked list queue is fast and constant time shows you understand efficient data structure design, a key skill in interviews.
"What if we did not keep a rear pointer and only had a front pointer? How would the time complexity change?"
Practice
Enqueue operation do in a linked list implementation of a queue?Solution
Step 1: Understand enqueue operation
Enqueue means adding an element to the queue.Step 2: Identify linked list behavior for enqueue
In a linked list queue, enqueue adds the new node at the end (tail).Final Answer:
Adds an element at the end of the linked list -> Option DQuick Check:
Enqueue = Add at tail [OK]
- Confusing enqueue with dequeue operation
- Adding element at the front instead of the end
- Thinking enqueue removes elements
Solution
Step 1: Create new node with data
Use 'new Node(data)' to create a node with the given data.Step 2: Link new node at tail and update tail
Set 'tail.next = newNode' and then update 'tail = newNode' to keep tail at last node.Final Answer:
Node newNode = new Node(data); tail.next = newNode; tail = newNode; -> Option AQuick Check:
Correct node creation and tail update [OK]
- Forgetting 'new' keyword when creating node
- Updating head instead of tail
- Assigning data to next pointer
QueueLinkedList queue = new QueueLinkedList(); queue.Enqueue(10); queue.Enqueue(20); queue.Enqueue(30);
Assuming the queue prints elements from head to tail.
Solution
Step 1: Enqueue 10, 20, 30 in order
Each enqueue adds element at the tail, so order is 10, then 20, then 30.Step 2: Print queue from head to tail
Head points to 10, followed by 20, then 30, ending with null.Final Answer:
10 -> 20 -> 30 -> null -> Option AQuick Check:
Enqueue order preserved from head to tail [OK]
- Reversing the order of elements
- Confusing head and tail pointers
- Assuming enqueue adds at head
public void Enqueue(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
tail.next = newNode;
}
}Solution
Step 1: Check tail update after enqueue
After adding newNode at tail.next, tail must be updated to newNode.Step 2: Identify missing tail update
Code does not update tail, so tail still points to old last node, causing errors.Final Answer:
The tail pointer is not updated after adding new node -> Option BQuick Check:
Always update tail after enqueue [OK]
- Forgetting to update tail pointer
- Updating head instead of tail
- Creating node outside enqueue method
Solution
Step 1: Enqueue 35 at tail
Queue becomes 5 -> 15 -> 25 -> 35 -> null after enqueue.Step 2: Dequeue removes from head
Removing 5 from head leaves 15 -> 25 -> 35 -> null.Final Answer:
15 -> 25 -> 35 -> null -> Option CQuick Check:
Enqueue adds tail, dequeue removes head [OK]
- Removing from tail instead of head
- Not updating head after dequeue
- Adding new element at head instead of tail
Ready to prove it?
Pass the test and this pattern moves unknown
