Create and Initialize Doubly Linked List in DSA Typescript - Time & Space Complexity
When we create and initialize a doubly linked list, we want to know how the time needed grows as we add more items.
We ask: How does the work change when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
prev: Node | null = null;
next: Node | null = null;
constructor(value: number) {
this.value = value;
}
}
function createDoublyLinkedList(values: number[]): Node | null {
if (values.length === 0) return null;
let head = new Node(values[0]);
let current = head;
for (let i = 1; i < values.length; i++) {
let newNode = new Node(values[i]);
current.next = newNode;
newNode.prev = current;
current = newNode;
}
return head;
}
This code creates a doubly linked list from an array of numbers by linking nodes forward and backward.
- Primary operation: The for-loop that creates and links each new node.
- How many times: It runs once for each element after the first in the input array.
Each new value requires creating a node and linking it, so the work grows directly with the number of values.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 node creations and links |
| 100 | About 100 node creations and links |
| 1000 | About 1000 node creations and links |
Pattern observation: The work increases evenly as the input size grows.
Time Complexity: O(n)
This means the time to create the list grows in a straight line with the number of items.
[X] Wrong: "Creating a doubly linked list takes more than linear time because of the backward links."
[OK] Correct: Each backward link is set immediately when the node is created, so it does not add extra loops or repeated work beyond the single pass.
Understanding this helps you explain how data structures build up step-by-step, showing you can analyze simple loops and link operations clearly.
"What if we added a nested loop inside the creation to check for duplicates? How would the time complexity change?"
Practice
Solution
Step 1: Understand node structure in doubly linked list
Each node in a doubly linked list contains data and two pointers: one to the previous node and one to the next node.Step 2: Compare with singly linked list
A singly linked list node has only one pointer to the next node, so it can only be traversed forward.Final Answer:
A doubly linked list has nodes with pointers to both previous and next nodes. -> Option DQuick Check:
Doubly linked list = two pointers per node [OK]
- Thinking doubly linked list uses arrays
- Assuming it can only go forward
- Confusing pointer usage
Solution
Step 1: Check for both next and prev pointers
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } defines both next and prev as Node or null, which is correct for doubly linked list nodes.Step 2: Verify constructor initialization
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } initializes next and prev to null, which is proper for a new node.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 BQuick Check:
Node class needs next and prev pointers [OK]
- Missing prev pointer in node class
- Using wrong types for pointers
- Initializing pointers with numbers instead of null
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
head: Node | null = null;
append(data: number) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
printForward() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new DoublyLinkedList();
list.append(10);
list.append(20);
list.append(30);
list.printForward();Solution
Step 1: Append nodes 10, 20, 30 in order
Nodes are added at the end, so list order is 10, then 20, then 30.Step 2: Print list forward
printForward traverses from head to next nodes, printing data with arrows, ending with null.Final Answer:
10 -> 20 -> 30 -> null -> Option AQuick Check:
Appending adds to tail, printForward shows 10 to 30 [OK]
- Confusing print order as reversed
- Missing last node in output
- Printing null at start instead of end
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
head: Node | null = null;
append(data: number) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
// Missing line here
}
}Solution
Step 1: Check append logic for doubly linked list
When appending, newNode.prev must point to current (the last node) to maintain backward link.Step 2: Identify missing pointer assignment
The code sets current.next = newNode but does not set newNode.prev = current, breaking backward link.Final Answer:
The code misses setting newNode.prev to current before appending. -> Option AQuick Check:
newNode.prev must link to previous node [OK]
- Forgetting to set newNode.prev
- Changing head incorrectly
- Checking wrong pointer in loop
Solution
Step 1: Understand reversing a doubly linked list by pointers
Reversing means for each node, next and prev pointers must be swapped to reverse direction.Step 2: Update head pointer
After swapping pointers, head must point to the original tail (last node) to reflect new start.Final Answer:
For each node, swap its next and prev pointers; then update head to last node. -> Option CQuick Check:
Swap next and prev, then reset head [OK]
- Only swapping data, not pointers
- Not updating head after reversal
- Setting pointers to null incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
