Dequeue Using Linked List in DSA Go - Time & Space Complexity
We want to understand how fast operations on a dequeue using a linked list run.
Specifically, how the time changes when we add or remove items from either end.
Analyze the time complexity of the following code snippet.
// Add item at front
func (dq *Dequeue) AddFront(value int) {
newNode := &Node{value: value}
newNode.next = dq.front
if dq.front != nil {
dq.front.prev = newNode
}
dq.front = newNode
if dq.rear == nil {
dq.rear = newNode
}
}
This code adds a new item at the front of the dequeue linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing a node at the front or rear pointer.
- How many times: Each operation runs once per call, no loops inside.
Each add or remove operation touches only a few nodes, no matter how big the dequeue is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 |
| 100 | 5 |
| 1000 | 5 |
Pattern observation: The number of steps stays about the same even if the dequeue grows larger.
Time Complexity: O(1)
This means adding or removing items at either end takes a constant amount of time, no matter the size.
[X] Wrong: "Adding or removing from a dequeue takes longer as it gets bigger because we have to move through the list."
[OK] Correct: We keep direct pointers to front and rear, so we never walk through the list. Operations happen instantly at those ends.
Knowing that dequeue operations run in constant time shows you understand efficient data structures, a key skill in coding interviews.
"What if we used a singly linked list without a rear pointer? How would the time complexity for adding at the rear change?"
Practice
Solution
Step 1: Understand dequeue operations
A dequeue supports insertion and deletion at both front and rear ends.Step 2: Compare linked list vs array
Linked list allows these operations in constant time without shifting elements, unlike arrays.Final Answer:
It allows insertion and deletion from both ends in constant time. -> Option CQuick Check:
Linked list dequeue = O(1) insert/delete both ends [OK]
- Thinking arrays allow O(1) insert/delete at both ends
- Confusing memory usage advantage
- Assuming automatic sorting
Solution
Step 1: Identify node fields for dequeue
A dequeue node needs data, pointer to next and previous nodes for double linking.Step 2: Check pointer usage in Go
Pointers to Node must be *Node, not Node, to avoid recursive struct size.Final Answer:
type Node struct { data int; next *Node; prev *Node } -> Option BQuick Check:
Double linked node uses *Node pointers [OK]
- Using Node instead of *Node for next/prev
- Missing prev pointer for double linking
- Declaring only next pointer
dq := NewDequeue() dq.InsertFront(10) dq.InsertRear(20) dq.InsertFront(5) dq.DeleteRear() dq.Display()Assuming Display prints elements from front to rear separated by ' -> ' and ends with ' -> null'.
Solution
Step 1: Trace insertions and deletions
InsertFront(10) => [10], InsertRear(20) => [10, 20], InsertFront(5) => [5, 10, 20], DeleteRear() removes 20 => [5, 10]Step 2: Determine display output
Display prints front to rear: 5 -> 10 -> nullFinal Answer:
5 -> 10 -> null -> Option AQuick Check:
After operations, dequeue = 5, 10 [OK]
- Forgetting DeleteRear removes last element
- Mixing order of InsertFront and InsertRear
- Printing rear to front instead of front to rear
func (dq *Dequeue) DeleteFront() {
if dq.front == nil {
fmt.Println("Dequeue is empty")
return
}
dq.front = dq.front.next
dq.front.prev = nil
}
What is the error in this code?Solution
Step 1: Analyze deletion when only one element exists
If only one node exists, after dq.front = dq.front.next, dq.front becomes nil.Step 2: Check rear pointer update
Rear pointer still points to old node; it must be set to nil to avoid dangling pointer.Final Answer:
It should set dq.rear to nil when the last element is deleted. -> Option DQuick Check:
Update rear on last element deletion [OK]
- Not updating rear when front becomes nil
- Assuming front.prev always exists
- Confusing prev and next pointers
Solution
Step 1: Understand in-place reversal of doubly linked list
Swapping next and prev pointers of each node reverses the links.Step 2: Update dequeue front and rear pointers
After swapping pointers, front and rear pointers must be swapped to reflect new ends.Final Answer:
Traverse each node, swap its next and prev pointers, then swap front and rear pointers of the dequeue. -> Option AQuick Check:
Swap pointers in nodes + swap front/rear [OK]
- Creating new dequeue instead of in-place reversal
- Swapping only data values without pointers
- Deleting and reinserting nodes unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
