Node Structure and Pointer Design in DSA Typescript - Time & Space Complexity
We want to understand how the design of nodes and pointers affects the speed of operations on linked data structures.
Specifically, how does the way nodes connect impact the time it takes to access or change data?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function traverseList(head: Node | null): void {
let current = head;
while (current !== null) {
console.log(current.value);
current = current.next;
}
}
This code defines a simple node with a pointer to the next node and a function to visit each node in the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through each node by following the next pointer.
- How many times: It runs once for each node in the list, so if there are n nodes, it runs n times.
As the number of nodes grows, the time to visit all nodes grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps to visit all nodes |
| 100 | 100 steps to visit all nodes |
| 1000 | 1000 steps to visit all nodes |
Pattern observation: The time grows directly with the number of nodes; doubling nodes doubles the steps.
Time Complexity: O(n)
This means the time to traverse the list grows linearly with the number of nodes.
[X] Wrong: "Accessing any node in the list is instant like an array index."
[OK] Correct: Because nodes are linked one after another, you must follow each pointer from the start to reach a node, so access time grows with position.
Understanding how node pointers affect traversal time helps you explain linked list operations clearly and shows you grasp fundamental data structure behavior.
"What if we added a pointer to the previous node (making it doubly linked)? How would the time complexity for traversal change?"
Practice
next pointer in a singly linked list node represent?Solution
Step 1: Understand the role of
Thenextin singly linked listnextpointer is used to link the current node to the following node in the list.Step 2: Differentiate from other pointers
Unlikeprevwhich points backward,nextalways points forward to the next node.Final Answer:
It points to the next node in the list -> Option DQuick Check:
nextpointer = next node [OK]
- Confusing next with previous pointer
- Thinking next stores data instead of address
- Assuming next holds list size
data property and a next pointer?Solution
Step 1: Check property types and initialization
Thenextpointer should allownullto represent the end of the list, so its type must beNode | null.Step 2: Verify constructor assigns values correctly
The constructor takesdataand setsnexttonullinitially.Final Answer:
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } -> Option BQuick Check:
Allow null for next pointer = class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } [OK]
- Not allowing next to be null
- Forgetting to initialize next in constructor
- Using wrong types for next pointer
class Node {
data: number;
next: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
}
}
const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
let current = head;
const result: number[] = [];
while (current !== null) {
result.push(current.data);
current = current.next;
}
console.log(result);Solution
Step 1: Trace node creation and linking
Nodes are created with data 10, 20, and 30. Thenextpointers link 10 -> 20 -> 30 -> null.Step 2: Follow the while loop to collect data
The loop starts at head (10), adds 10, moves to 20, adds 20, moves to 30, adds 30, then stops at null.Final Answer:
[10, 20, 30] -> Option AQuick Check:
Traverse all nodes = [10, 20, 30] [OK]
- Stopping traversal too early
- Not linking nodes correctly
- Confusing data values with pointers
class Node {
data: number;
next: Node;
constructor(data: number) {
this.data = data;
this.next = null;
}
}Solution
Step 1: Analyze the type of next pointer
Thenextproperty is typed asNodebut assignednull, which is invalid.Step 2: Correct the type to allow null
Change the type toNode | nullso it can hold either a node or null.Final Answer:
The type of next should be 'Node | null' to allow null assignment -> Option CQuick Check:
Allow null in next type = The type of next should be 'Node | null' to allow null assignment [OK]
- Not allowing null in next type
- Removing constructor unnecessarily
- Making data optional without reason
Solution
Step 1: Check pointer types for doubly linked list
Bothprevandnextpointers must allownullto represent ends of the list.Step 2: Verify constructor initializes pointers to null
Constructor correctly setsprevandnexttonullinitially.Final Answer:
class Node { data: number; prev: Node | null; next: Node | null; constructor(data: number) { this.data = data; this.prev = null; this.next = null; } } -> Option AQuick Check:
Doubly linked list pointers allow null = class Node { data: number; prev: Node | null; next: Node | null; constructor(data: number) { this.data = data; this.prev = null; this.next = null; } } [OK]
- Not allowing null for prev or next
- Setting pointer types incorrectly
- Initializing pointers to wrong values
Ready to prove it?
Pass the test and this pattern moves unknown
