Why Doubly Linked List Over Singly Linked List in DSA Typescript - Performance Analysis
We want to understand how using a doubly linked list changes the time it takes to do common operations compared to a singly linked list.
Which operations become faster or slower, and why?
Analyze the time complexity of traversing and deleting nodes in singly vs doubly linked lists.
class Node {
value: number;
next: Node | null;
prev: Node | null; // only in doubly linked list
constructor(value: number) {
this.value = value;
this.next = null;
this.prev = null;
}
}
// Example: delete a node when given a pointer to it
function deleteNode(node: Node) {
if (node.prev) node.prev.next = node.next;
if (node.next) node.next.prev = node.prev;
}
This code shows how a node is deleted in a doubly linked list using both next and prev pointers.
Look at what operations repeat when traversing or deleting nodes.
- Primary operation: Moving from one node to the next (traversal).
- How many times: Up to n times for traversal through the list of size n.
- Deletion operation: In doubly linked list, deletion uses direct access to previous node; in singly linked list, it requires traversal to find previous node.
Traversal takes time proportional to the number of nodes.
| Input Size (n) | Traversal Steps | Deletion Steps (given node) |
|---|---|---|
| 10 | 10 steps | Doubly: 1 step, Singly: up to 9 steps |
| 100 | 100 steps | Doubly: 1 step, Singly: up to 99 steps |
| 1000 | 1000 steps | Doubly: 1 step, Singly: up to 999 steps |
Pattern observation: Traversal grows linearly with n in both lists, but deletion is constant time in doubly linked list and linear in singly linked list when previous node is unknown.
Time Complexity: O(n) for traversal, O(1) for deletion in doubly linked list, O(n) for deletion in singly linked list.
This means doubly linked lists allow faster deletion when you have a pointer to the node, because you can access the previous node directly.
[X] Wrong: "Doubly linked lists always take twice as long because they have two pointers per node."
[OK] Correct: While doubly linked lists use more memory, their operations like deletion can be faster because you don't need to search for the previous node, saving time especially in large lists.
Understanding when to use doubly linked lists shows you know how to balance memory use and speed. This skill helps you choose the right tool for the job in real coding tasks.
"What if we only needed to traverse the list forward and never delete nodes? How would the time complexity difference between singly and doubly linked lists change?"
Practice
doubly linked list over a singly linked list in a program?Solution
Step 1: Understand traversal capabilities
A singly linked list allows moving forward only, while a doubly linked list allows moving forward and backward.Step 2: Compare memory and complexity
Doubly linked lists use more memory and are more complex but provide two-way traversal.Final Answer:
Because it allows traversal in both directions easily -> Option BQuick Check:
Two-way traversal = A [OK]
- Thinking doubly linked lists use less memory
- Confusing simplicity with singly linked lists
- Assuming singly linked lists allow backward traversal
Solution
Step 1: Identify required pointers for doubly linked list node
A doubly linked list node must have bothnextandprevpointers, which can be null if no neighbor exists.Step 2: Check TypeScript syntax for nullable pointers
UsingNode | nullcorrectly allows for the end nodes to have null pointers.Final Answer:
class Node { value: number; next: Node | null; prev: Node | null; } -> Option CQuick Check:
Doubly linked node needs next and prev with nulls [OK]
- Omitting the prev pointer
- Not allowing null for next or prev
- Using only one pointer like singly linked list
class Node {
value: number;
next: Node | null = null;
prev: Node | null = null;
constructor(value: number) { this.value = value; }
}
class DoublyLinkedList {
head: Node | null = null;
insertFront(value: number) {
const newNode = new Node(value);
if (this.head) {
newNode.next = this.head;
this.head.prev = newNode;
}
this.head = newNode;
}
}
const list = new DoublyLinkedList();
list.insertFront(10);
list.insertFront(20);
list.insertFront(30);
let current = list.head;
const result = [];
while (current) {
result.push(current.value);
current = current.next;
}
console.log(result);
What will be printed?Solution
Step 1: Trace insertFront calls
First insert 10: list is 10. Then insert 20 at front: list is 20 -> 10. Then insert 30 at front: list is 30 -> 20 -> 10.Step 2: Traverse from head to next
Starting at head (30), next is 20, then 10, then null. So collected values are [30, 20, 10].Final Answer:
[30, 20, 10] -> Option DQuick Check:
Insert front reverses order = [30, 20, 10] [OK]
- Assuming insertion appends at end
- Mixing order of traversal
- Ignoring prev pointer effects
deleteLast() {
if (!this.head) return;
let current = this.head;
while (current.next) {
current = current.next;
}
current.prev.next = null;
}
What is the bug here?Solution
Step 1: Analyze deletion logic
The code moves to the last node by following next pointers. Then it setscurrent.prev.next = nullto remove last node.Step 2: Check edge case for single node list
If list has only one node,current.previs null, socurrent.prev.nextcauses error.Final Answer:
It does not handle the case when list has only one node -> Option AQuick Check:
Single node list needs special case [OK]
- Ignoring single node edge case
- Assuming prev is always non-null
- Confusing next and prev pointers
Solution
Step 1: Understand backward traversal need
Backward traversal means moving from a node to its previous nodes efficiently.Step 2: Compare data structures for backward traversal
Singly linked lists do not have backward pointers. Arrays allow random access but not linked traversal. Circular singly linked lists loop but do not support backward traversal. Doubly linked lists have prev pointers for efficient backward moves.Final Answer:
Doubly linked list because it allows backward traversal efficiently -> Option AQuick Check:
Backward traversal needs prev pointers [OK]
- Choosing singly linked list for backward traversal
- Confusing arrays with linked lists
- Thinking circular singly linked lists support backward moves
Ready to prove it?
Pass the test and this pattern moves unknown
