Insert at Beginning of Doubly Linked List in DSA Javascript - Time & Space Complexity
We want to know how the time to insert a new node at the start of a doubly linked list changes as the list grows.
How does the number of steps needed grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
function insertAtBeginning(head, data) {
const newNode = new Node(data);
newNode.next = head;
if (head !== null) {
head.prev = newNode;
}
return newNode;
}
This code creates a new node and inserts it at the start of a doubly linked list, updating pointers accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating a few pointers.
- How many times: This happens once per insertion, no loops or recursion involved.
Adding a node at the start always takes the same number of steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4-5 steps |
| 100 | 4-5 steps |
| 1000 | 4-5 steps |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means inserting at the beginning takes a constant amount of time no matter how large the list is.
[X] Wrong: "Inserting at the start takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a few pointers at the start; no need to move or touch other nodes.
Knowing that insertion at the start is quick helps you choose the right data structure and shows you understand efficient pointer updates.
"What if we changed this to insert at the end of the doubly linked list without a tail pointer? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head node position
The head node is the first node in the doubly linked list.Step 2: Insert new node before head
Inserting at the beginning means placing the new node before the current head, making it the new head.Final Answer:
To add a new node before the current head node -> Option AQuick Check:
Insert at beginning = add before head [OK]
- Confusing insertion at beginning with insertion at end
- Thinking insertion removes nodes
- Mixing up head and tail nodes
Solution
Step 1: Identify the old head's previous pointer
The old head's previous pointer should point to the new node after insertion.Step 2: Update old head's prev to new node
Setting oldHead.prev = newNode links the old head back to the new node.Final Answer:
oldHead.prev = newNode; -> Option AQuick Check:
Old head prev points to new node [OK]
- Setting oldHead.next instead of prev
- Confusing newNode's pointers
- Assigning newNode.next incorrectly
Solution
Step 1: Insert 10 before current head (5)
The new node with value 10 becomes the new head, pointing next to 5.Step 2: Link the rest of the list after 5
The list continues as 5 <-> 15 <-> 20 unchanged after insertion.Final Answer:
10 <-> 5 <-> 15 <-> 20 -> Option CQuick Check:
New head 10 followed by old list [OK]
- Appending new node at end instead of beginning
- Swapping nodes incorrectly
- Ignoring pointer updates
function insertAtBeginning(head, data) {
const newNode = { data: data, prev: null, next: head };
head.prev = newNode;
head = newNode;
return head;
}Solution
Step 1: Check for empty list case
If head is null, head.prev causes error because head is null.Step 2: Missing null check before setting head.prev
Code should check if head exists before setting head.prev = newNode.Final Answer:
It does not handle empty list when head is null -> Option BQuick Check:
Null head requires special handling [OK]
- Ignoring empty list case
- Assuming head is never null
- Not returning updated head
Solution
Step 1: Insert 3 before current head 7
The new node 3 becomes the head, pointing next to 7.Step 2: Traverse forward and backward
Forward traversal: 3 -> 7 -> 14 -> 21; backward traversal reverses this order.Final Answer:
Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3 -> Option DQuick Check:
New head first, tail last, pointers reversed backward [OK]
- Mixing forward and backward traversal order
- Appending new node at tail instead of head
- Incorrect pointer updates causing wrong traversal
Ready to prove it?
Pass the test and this pattern moves unknown
