Enqueue Using Linked List in DSA Javascript - 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 enqueue operation using a linked list.
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
enqueue(value) {
const newNode = new Node(value);
if (!this.rear) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}
}
This code adds a new item to the end of a queue implemented with a linked list.
Look for loops or repeated steps inside the enqueue method.
- Primary operation: Adding a new node and updating pointers.
- How many times: This happens once per enqueue call, no loops involved.
Each enqueue adds one node by updating 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 number of steps 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, a key skill in coding interviews.
"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 queue implemented using a linked list?Solution
Step 1: Understand enqueue operation
Enqueue means adding an element to the queue.Step 2: Identify position in linked list
In a linked list queue, new elements are added at the tail (end).Final Answer:
Adds a new element at the end (tail) of the linked list -> Option CQuick Check:
Enqueue = add at tail [OK]
- Confusing enqueue with dequeue operation
- Adding new element at the head instead of tail
- Thinking enqueue removes elements
Solution
Step 1: Understand node structure
A node should have a value and a pointer to next (null initially).Step 2: Check syntax correctness
const node = { value: val, next: null }; correctly creates an object with value and next set to null.Final Answer:
const node = { value: val, next: null }; -> Option AQuick Check:
Node = {{value, next:null}} [OK]
- Using wrong property names like 'val' instead of 'value'
- Setting next to undefined instead of null
- Calling Node as a function without new keyword
enqueue(10); enqueue(20); enqueue(30);Assuming an empty queue initially, what is the linked list representation?
Solution
Step 1: Enqueue 10
Queue is empty, so 10 becomes head and tail: 10 -> nullStep 2: Enqueue 20 and 30
20 added at tail: 10 -> 20 -> null, then 30 added: 10 -> 20 -> 30 -> nullFinal Answer:
10 -> 20 -> 30 -> null -> Option AQuick Check:
Order preserved, enqueue adds at tail [OK]
- Reversing order of elements
- Confusing head and tail positions
- Assuming enqueue adds at head
function enqueue(val) {
const node = { value: val, next: null };
if (!this.tail) {
this.head = node;
}
this.tail.next = node;
this.tail = node;
}What error will occur when enqueue is called on an empty queue?
Solution
Step 1: Analyze empty queue condition
When queue is empty, this.tail is null or undefined.Step 2: Identify error line
Code tries this.tail.next = node; but this.tail is null, causing TypeError.Final Answer:
TypeError: Cannot read property 'next' of null -> Option DQuick Check:
Accessing next on null causes TypeError [OK]
- Ignoring null check for tail
- Assuming no error on empty queue
- Confusing this.head and this.tail usage
val?Solution
Step 1: Handle empty queue case
If head is null, both head and tail should point to new node.Step 2: Handle non-empty queue
Set tail.next to new node and update tail to new node.Final Answer:
const node = { value: val, next: null }; if (!this.head) { this.head = node; this.tail = node; } else { this.tail.next = node; this.tail = node; } -> Option BQuick Check:
Check empty and non-empty cases for enqueue [OK]
- Not updating tail on empty queue
- Adding new node at head instead of tail
- Forgetting to update tail pointer
Ready to prove it?
Pass the test and this pattern moves unknown
