Doubly Linked List Structure and Node Design in DSA Javascript - 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 add, remove, or find nodes as the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
}
This code defines the basic structure of a doubly linked list node and the list itself, setting up pointers for forward and backward links.
Since this snippet only sets up the structure, no loops or traversals happen here.
- Primary operation: Creating nodes and linking pointers.
- How many times: Each node creation is a single operation; no repetition yet.
Because this snippet only defines the structure, the time to create one node is constant regardless of list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 node creations, each constant time |
| 100 | 100 node creations, each constant time |
| 1000 | 1000 node creations, each constant time |
Pattern observation: Each node creation takes the same small amount of time, so total time grows linearly with number of nodes created.
Time Complexity: O(1) per node creation
This means creating or initializing a single node takes the same amount of time no matter how big the list is.
[X] Wrong: "Creating a node takes longer as the list grows because it has to check all existing nodes."
[OK] Correct: Creating a node only sets its own pointers; it does not traverse or check other nodes, so time stays constant.
Understanding the basic node structure and its constant creation time helps build confidence for more complex list operations.
"What if we added a method to find a node by value? How would the time complexity change then?"
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 next node and one to the previous node.Final Answer:
A doubly linked list node has pointers to both the next and previous nodes. -> Option DQuick Check:
Doubly linked list nodes have two pointers [OK]
- Thinking doubly linked list nodes store data differently
- Confusing the number of pointers in singly vs doubly linked nodes
- Assuming doubly linked nodes do not store data
Solution
Step 1: Check for data property in constructor
The node must store data, so constructor should assign this.data = data.Step 2: Check for both next and prev pointers
A doubly linked list node must have this.next and this.prev initialized to null.Final Answer:
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } -> Option BQuick Check:
Doubly linked list node has data, next, and prev [OK]
- Omitting the prev pointer
- Not initializing next or prev to null
- Forgetting to assign data in constructor
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
}
insert(data) {
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;
}
print() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' <-> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new DoublyLinkedList();
list.insert(10);
list.insert(20);
list.print();Solution
Step 1: Insert 10 into empty list
Since head is null, head becomes node with data 10.Step 2: Insert 20 at the end
Traverse to last node (10), set its next to new node 20, and new node's prev to 10.Step 3: Print list
Traverse from head: 10 <-> 20 <-> null is printed.Final Answer:
10 <-> 20 <-> null -> Option AQuick Check:
Insertion appends nodes in order [OK]
- Confusing order of nodes after insertion
- Using single arrow -> instead of double <->
- Printing null instead of list contents
insert(data) {
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: Understand pointer updates in insertion
When adding newNode after current, newNode.prev must point back to current.Step 2: Identify missing pointer assignment
Code sets current.next = newNode but does not set newNode.prev = current, breaking backward link.Final Answer:
Add 'newNode.prev = current;' after 'current.next = newNode;' -> Option AQuick Check:
Backward link must be set for doubly linked list [OK]
- Forgetting to set newNode.prev
- Setting current.prev instead of newNode.prev
- Assuming no fix needed
Solution
Step 1: Understand deletion pointer updates
To remove a node, update previous node's next and next node's prev to bypass it.Step 2: Check necessity of clearing deleted node pointers
Setting deleted node's next and prev to null helps garbage collection and avoids dangling pointers.Step 3: Analyze traversal direction
Traversing backwards from tail is not necessary; forward traversal from head suffices to find node.Final Answer:
Traverse the list backwards from tail to find the node. -> Option CQuick Check:
Forward traversal is enough to find node for deletion [OK]
- Thinking backward traversal is required
- Ignoring pointer updates on neighbors
- Not clearing deleted node pointers
Ready to prove it?
Pass the test and this pattern moves unknown
