Dequeue Using Linked List in DSA C# - Time & Space Complexity
We want to understand how the time to add or remove items in a dequeue using a linked list changes as the number of items grows.
How does the time needed grow when the dequeue gets bigger?
Analyze the time complexity of the following code snippet.
public class Node {
public int Value;
public Node Next, Prev;
public Node(int val) { Value = val; }
}
public class Dequeue {
private Node head, tail;
public void AddFront(int val) {
Node newNode = new Node(val);
if (head == null) { head = tail = newNode; return; }
newNode.Next = head;
head.Prev = newNode;
head = newNode;
}
public void RemoveFront() {
if (head == null) return;
head = head.Next;
if (head != null) head.Prev = null;
else tail = null;
}
}
This code adds and removes items at the front of a dequeue implemented with a linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing a node at the front involves only a few pointer changes.
- How many times: Each add or remove runs once per call, no loops or recursion inside.
Each add or remove operation takes the same small number of steps no matter how many items are in the dequeue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5-6 steps |
| 100 | 5-6 steps |
| 1000 | 5-6 steps |
Pattern observation: The time stays about the same no matter how big the dequeue is.
Time Complexity: O(1)
This means adding or removing at the front takes a constant amount of time regardless of the dequeue size.
[X] Wrong: "Removing from the front takes longer as the dequeue grows because we have to move through all nodes."
[OK] Correct: We keep a direct pointer to the front node, so removal only changes a few pointers without walking the list.
Knowing that linked list dequeue operations run in constant time shows you understand efficient data structure design, a key skill in interviews.
"What if we had to find a node by value before removing it? How would the time complexity change?"
Practice
Dequeue using a linked list compared to an array?Solution
Step 1: Understand Dequeue operations
A dequeue allows adding and removing elements from both front and rear ends.Step 2: Compare linked list vs array
Linked list supports constant time insertion and deletion at both ends, unlike arrays which may require shifting elements.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) insertion at front
- Confusing memory usage advantage
- Assuming automatic sorting
Solution
Step 1: Check access modifiers and fields
For linked list nodes, fields should be public to access from dequeue class.Step 2: Verify presence of next and prev pointers
Doubly linked list nodes need both next and prev references.Final Answer:
public class Node { public int data; public Node next; public Node prev; } -> Option BQuick Check:
Public fields + next + prev = correct node class [OK]
- Missing public keyword for fields
- Using struct instead of class
- Omitting prev pointer
Dequeue dq = new Dequeue(); dq.InsertFront(10); dq.InsertRear(20); dq.InsertFront(5); dq.DeleteRear(); dq.Display();
Solution
Step 1: Trace insertions
InsertFront(10) adds 10 at front: 10 -> null InsertRear(20) adds 20 at rear: 10 -> 20 -> null InsertFront(5) adds 5 at front: 5 -> 10 -> 20 -> nullStep 2: Trace deletion and display
DeleteRear() removes 20 from rear: 5 -> 10 -> null Display prints elements from front to rear.Final Answer:
5 -> 10 -> null -> Option DQuick Check:
After delete rear, list = 5 -> 10 [OK]
- Forgetting delete rear removes last element
- Mixing front and rear insertions
- Incorrect display order
public void DeleteFront() {
if (front == null) return;
front = front.next;
front.prev = null;
}Solution
Step 1: Analyze deletion steps
After moving front to front.next, if front becomes null (list empty), front.prev = null causes NullReferenceException.Step 2: Identify missing null check
Code must check if front is null before setting front.prev = null to avoid error.Final Answer:
It does not handle the case when the list becomes empty after deletion. -> Option AQuick Check:
Null check needed after front update [OK]
- Ignoring null after front update
- Updating rear pointer incorrectly
- Setting pointers in wrong order
ReverseDisplay() for a dequeue using a doubly linked list that prints elements from rear to front. Which approach is correct?Solution
Step 1: Understand doubly linked list structure
Each node has next and prev pointers; rear node's next is null, front node's prev is null.Step 2: Reverse traversal logic
To print from rear to front, start at rear and move backward using prev pointers until null.Final Answer:
Start from rear node and traverse using prev pointers until null, printing each node's data. -> Option AQuick Check:
Traverse backward from rear using prev [OK]
- Traversing next pointers from front for reverse
- Swapping pointers incorrectly
- Using recursion unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
