Enqueue Using Linked List in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to add an item to a queue changes as the queue grows.
Specifically, how long does it take to add one element at the end of a linked list?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null = null;
constructor(value: number) {
this.value = value;
}
}
class Queue {
tail: Node | null = null;
head: Node | null = null;
enqueue(value: number) {
const newNode = new Node(value);
if (this.tail) {
this.tail.next = newNode;
this.tail = newNode;
} else {
this.head = newNode;
this.tail = newNode;
}
}
}
This code adds a new item to the end of a linked list representing a queue.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating the tail pointer.
- How many times: Exactly once per enqueue call, no loops or traversals.
Adding one item always takes the same steps, no matter how big the queue is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5-6 steps |
| 100 | 5-6 steps |
| 1000 | 5-6 steps |
Pattern observation: The time to add does not grow with the queue size; it stays constant.
Time Complexity: O(1)
This means adding an item takes the same short time no matter how many items are already in the queue.
[X] Wrong: "Adding an item takes longer as the queue grows because we must find the end each time."
[OK] Correct: Because we keep a direct pointer to the end (tail), we do not search; we add instantly.
Knowing this helps you explain why linked lists are good for queues and shows you understand efficient data structure design.
"What if we did not keep a tail 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
Enqueue means adding an element to the queue.Step 2: Identify linked list position for enqueue
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 BQuick Check:
Enqueue = Add at tail [OK]
- Confusing enqueue with dequeue operation
- Adding new element at head instead of tail
- Thinking enqueue removes elements
- Mixing linked list positions
Solution
Step 1: Recall TypeScript class instantiation
To create an object, usenew ClassName(params).Step 2: Check options for correct syntax
Onlyconst newNode = new Node(value);correctly creates a new Node instance.Final Answer:
const newNode = new Node(value); -> Option CQuick Check:
Use 'new' keyword to instantiate class [OK]
- Omitting 'new' keyword when creating objects
- Using function call syntax without 'new'
- Incorrect property access like Node.new
- Not declaring variable with let or const
enqueue(10); enqueue(20); enqueue(30);
Solution
Step 1: Understand enqueue order
Enqueue adds elements at the tail, so order is preserved as inserted.Step 2: Trace each enqueue
After enqueue(10): 10 -> null
After enqueue(20): 10 -> 20 -> null
After enqueue(30): 10 -> 20 -> 30 -> nullFinal Answer:
10 -> 20 -> 30 -> null -> Option AQuick Check:
Enqueue order = insertion order [OK]
- Reversing order of elements after enqueue
- Confusing enqueue with push at head
- Misreading linked list direction
- Ignoring null at the end
enqueue(value: number) {
const newNode = new Node(value);
if (!this.tail) {
this.head = newNode;
this.tail = newNode;
return;
}
this.tail.next = newNode;
this.tail = newNode;
}What is the bug in this code?
Solution
Step 1: Analyze enqueue logic
When queue is empty, head and tail should point to newNode.Step 2: Check linking of nodes
Code sets tail = newNode but does not link old tail's next to newNode, breaking chain.Final Answer:
It does not update the previous tail's next pointer to newNode -> Option AQuick Check:
Link old tail's next to newNode [OK]
- Forgetting to update old tail's next pointer
- Not handling empty queue case properly
- Setting tail to null accidentally
- Creating node without value parameter
After enqueue(35), what will be the correct linked list representation and tail node value?
Solution
Step 1: Understand enqueue effect on queue
Enqueue adds new element at tail, so 35 goes after 25.Step 2: Update tail pointer
Tail must point to the new last node, which holds 35.Final Answer:
5 -> 15 -> 25 -> 35 -> null, tail value = 35 -> Option DQuick Check:
Enqueue adds at tail, tail updates to new node [OK]
- Adding new node at head instead of tail
- Not updating tail pointer after enqueue
- Confusing tail value with head value
- Leaving tail pointing to old last node
Ready to prove it?
Pass the test and this pattern moves unknown
