Enqueue Using Linked List in DSA Java - Time & Space Complexity
We want to understand how long it takes to add an item to a queue when using a linked list.
Specifically, how the time changes as the queue grows larger.
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class Queue {
Node front, rear;
void enqueue(int value) {
Node newNode = new Node(value);
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 adds one node by changing a few pointers, no matter how big the queue is.
| 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 many items are in the queue.
Time Complexity: O(1)
This means adding an item takes the same small amount of time, no matter how big the queue is.
[X] Wrong: "Enqueue takes longer as the queue grows because we have to find the end each time."
[OK] Correct: We keep a pointer to the end (rear), so we add directly without searching, keeping time constant.
Knowing that enqueue in a linked list queue is fast and constant time shows you understand efficient data structure design.
"What if we did not keep a rear pointer and had to find the end each time? How would the time complexity change?"
Practice
enqueue operation do in a linked list implementation of a queue?Solution
Step 1: Understand enqueue operation in queue
Enqueue means adding an element to the queue, which is done at the rear end.Step 2: Linked list enqueue adds at the rear
In linked list, adding at the rear means inserting the new node at the end of the list.Final Answer:
Adds an element at the end (rear) of the linked list -> Option AQuick Check:
Enqueue = add at rear [OK]
- Confusing enqueue with dequeue operation
- Thinking enqueue adds at the front
- Mixing linked list head and tail roles
Solution
Step 1: Understand Java object creation syntax
In Java, to create an object with a constructor that takes a value, usenew Node(value).Step 2: Check next pointer initialization
After creating the node, setnexttonullto mark end of list.Final Answer:
Node newNode = new Node(value); newNode.next = null; -> Option CQuick Check:
Java new object with constructor = new Node(value) [OK]
- Missing 'new' keyword when creating object
- Incorrect constructor call syntax
- Not setting next pointer to null
enqueue(10); enqueue(20); enqueue(30);
Solution
Step 1: Enqueue 10 on empty queue
Queue becomes: 10 -> nullStep 2: Enqueue 20 and 30 sequentially
After enqueue(20): 10 -> 20 -> null
After enqueue(30): 10 -> 20 -> 30 -> nullFinal Answer:
10 -> 20 -> 30 -> null -> Option AQuick Check:
Enqueue adds at rear, order preserved [OK]
- Reversing order of elements after enqueue
- Confusing enqueue with push (stack)
- Assuming new elements added at front
void enqueue(int value) {
Node newNode = new Node(value);
if (rear == null) {
front = newNode;
} else {
rear.next = newNode;
}
}Solution
Step 1: Analyze enqueue logic
When queue is empty, front is set to newNode but rear is not updated.Step 2: Check rear pointer update
After adding newNode, rear should point to newNode to keep track of last element.Final Answer:
Missing update of rear pointer to newNode -> Option BQuick Check:
Rear must always point to last node [OK]
- Forgetting to update rear pointer
- Updating front.next instead of rear.next
- Not handling empty queue case properly
5 -> 15 -> 25 -> null, what will be the queue state after enqueueing 35 and then dequeueing one element?Solution
Step 1: Enqueue 35 at rear
Queue becomes: 5 -> 15 -> 25 -> 35 -> nullStep 2: Dequeue removes front element 5
After dequeue: 15 -> 25 -> 35 -> nullFinal Answer:
15 -> 25 -> 35 -> null -> Option DQuick Check:
Enqueue adds rear, dequeue removes front [OK]
- Removing wrong element on dequeue
- Adding new element at front instead of rear
- Not updating front pointer after dequeue
Ready to prove it?
Pass the test and this pattern moves unknown
