Doubly Linked List Structure and Node Design in DSA Typescript - Time & Space Complexity
We want to understand how the time to perform operations on a doubly linked list changes as the list grows.
Specifically, how fast can we access or update nodes in this structure?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null;
prev: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
this.prev = null;
}
}
This code defines a node in a doubly linked list with pointers to the next and previous nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing or traversing nodes by following next or prev pointers.
- How many times: Depends on how many nodes we move through, up to n times for n nodes.
When we traverse the list, the time grows linearly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach a node at the end |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time to reach a node grows directly with the list size.
Time Complexity: O(n)
This means the time to access or traverse nodes grows in direct proportion to the number of nodes.
[X] Wrong: "Accessing any node in a doubly linked list is instant like an array."
[OK] Correct: Unlike arrays, linked lists require moving step-by-step through nodes, so access time depends on position.
Understanding how node access time grows helps you explain linked list behavior clearly and shows you know how data structures work under the hood.
"What if we added a hash map to store node references? How would the time complexity for access change?"
Practice
Solution
Step 1: Understand node pointers in singly linked list
A singly linked list node has only one pointer to the next node.Step 2: Understand node pointers in doubly linked list
A doubly linked list node has two pointers: one to the previous node and one to the next node.Final Answer:
A doubly linked list node has pointers to both the previous and next nodes. -> Option AQuick Check:
Doubly linked list nodes have two pointers [OK]
- Thinking doubly linked nodes only point forward
- Confusing data storage with pointer structure
- Assuming doubly linked nodes do not store data
Solution
Step 1: Check property types for next and prev
Since next and prev can be null initially, their types must allow null (Node | null).Step 2: Verify constructor initializes all properties
The constructor sets data and initializes next and prev to null correctly.Final Answer:
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } -> Option AQuick Check:
Allow null for next and prev in TypeScript [OK]
- Not allowing next and prev to be null
- Forgetting to initialize next and prev in constructor
- Setting next and prev types incorrectly
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
const node1 = new Node(10);
const node2 = new Node(20);
node1.next = node2;
node2.prev = node1;
console.log(node1.next?.data);
console.log(node2.prev?.data);Solution
Step 1: Trace node1.next and node2.prev assignments
node1.next points to node2, so node1.next.data is 20. node2.prev points to node1, so node2.prev.data is 10.Step 2: Check console.log outputs
console.log(node1.next?.data) prints 20, console.log(node2.prev?.data) prints 10.Final Answer:
20 10 -> Option DQuick Check:
next and prev pointers give 20 and 10 respectively [OK]
- Confusing which node data is printed
- Ignoring optional chaining and assuming errors
- Mixing up prev and next pointers
class Node {
data: number;
next: Node;
prev: Node;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}Solution
Step 1: Analyze type declarations and assignments
next and prev are typed as Node but assigned null, which is invalid in TypeScript.Step 2: Fix type to allow null
Changing next and prev types to 'Node | null' allows null assignment and fixes the error.Final Answer:
Change next and prev types to 'Node | null' to allow null assignment. -> Option CQuick Check:
Nullable pointers need 'Node | null' type [OK]
- Leaving next and prev as non-nullable types
- Trying to assign new Node() without data
- Removing constructor unnecessarily
Solution
Step 1: Set newNode pointers to link between nodeA and nodeB
newNode.prev must point to nodeA and newNode.next must point to nodeB to fit between them.Step 2: Update nodeA and nodeB pointers to include newNode
nodeA.next should point to newNode and nodeB.prev should point to newNode to complete the insertion.Final Answer:
newNode.prev = nodeA; newNode.next = nodeB; nodeA.next = newNode; nodeB.prev = newNode; -> Option BQuick Check:
Link newNode between nodeA and nodeB correctly [OK]
- Mixing up prev and next pointers
- Updating neighbors before setting newNode pointers
- Reversing nodeA and nodeB in assignments
Ready to prove it?
Pass the test and this pattern moves unknown
