Insert at Beginning of Circular Linked List in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to insert a new node at the start of a circular linked list changes as the list grows.
How does the number of steps grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
function insertAtBeginning(head, data) {
const newNode = { data: data, next: null };
if (!head) {
newNode.next = newNode;
return newNode;
}
let temp = head;
while (temp.next !== head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
return newNode;
}
This code inserts a new node at the start of a circular linked list by first finding the last node, then linking the new node before the head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop that moves through the list to find the last node. - How many times: It runs once for each node until it reaches the last node, so it runs
ntimes if the list hasnnodes.
As the list grows, the loop runs more times because it must find the last node each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to find last node |
| 100 | About 100 steps to find last node |
| 1000 | About 1000 steps to find last node |
Pattern observation: The number of steps grows directly with the size of the list.
Time Complexity: O(n)
This means the time to insert at the beginning grows linearly with the number of nodes in the list.
[X] Wrong: "Inserting at the beginning is always instant, so it must be O(1)."
[OK] Correct: Because we must find the last node to update its pointer, the operation depends on the list size, making it O(n).
Understanding this helps you explain how linked list operations work and why some seem fast but actually depend on list size. This skill shows you know how data structures behave in real code.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the circular linked list structure
In a circular linked list, the last node points back to the head node, forming a circle.Step 2: Insert new node at beginning
The new node is linked to the current head, and the last node's next pointer is updated to this new node, making it the new head.Final Answer:
The new node becomes the head and points to the old head, maintaining the circular link. -> Option AQuick Check:
New head points to old head, circle intact [OK]
- Forgetting to update last node's next pointer
- Breaking the circular link by not linking last node to new head
- Inserting at end instead of beginning
Solution
Step 1: Create new node with next pointing to current head
The new node's next should point to the current head to insert at beginning.Step 2: Update last node's next to new node and update head
Since it's circular, last node's next must point to new node, then head is updated to new node.Final Answer:
const newNode = { data: value, next: head }; last.next = newNode; head = newNode; -> Option AQuick Check:
New node next = head, last.next = new node, head updated [OK]
- Not updating last.next to new node
- Setting newNode.next to null instead of head
- Updating head before last.next
function Node(data) { this.data = data; this.next = null; }
let head = new Node(20);
let second = new Node(30);
head.next = second;
second.next = head; // circular link
// Insert 10 at beginning
const newNode = new Node(10);
newNode.next = head;
second.next = newNode;
head = newNode;
// Print list starting from head
let temp = head;
do {
console.log(temp.data);
temp = temp.next;
} while(temp !== head);Solution
Step 1: Understand the initial circular list
Initially, list is 20 -> 30 -> back to 20.Step 2: Insert 10 at beginning and update links
New node 10 points to 20 (old head), 30 (last node) points to 10, head updated to 10.Step 3: Print order starting from new head
Traversal prints 10, then 20, then 30, then stops as it loops back to head.Final Answer:
10 20 30 -> Option BQuick Check:
New head 10, then 20, then 30 [OK]
- Not updating last node's next to new head
- Printing nodes until null instead of circular condition
- Confusing node order after insertion
function insertAtBeginning(head, value) {
const newNode = { data: value, next: head };
let temp = head;
while(temp.next !== null) {
temp = temp.next;
}
temp.next = newNode;
head = newNode;
return head;
}Solution
Step 1: Analyze loop condition for circular list
In a circular linked list, last node's next points to head, not null, so loop should check temp.next !== head.Step 2: Understand impact of wrong condition
Checking for null causes infinite loop or error because next is never null in circular list.Final Answer:
The while loop condition should check for temp.next !== head instead of null. -> Option DQuick Check:
Loop until last node points to head [OK]
- Using null check in circular list traversal
- Not updating last node's next pointer correctly
- Forgetting to update head after insertion
Solution
Step 1: Insert new node at beginning and update pointers
New node 10 points to old head 5; last node 25 must point to new node 10 to keep circular link.Step 2: Traverse list from new head
Traversal order is 10 -> 5 -> 15 -> 25, then back to 10.Step 3: Importance of updating last.next
If last.next is not updated to new head, circular link breaks and traversal fails.Final Answer:
10 -> 5 -> 15 -> 25; updating last.next maintains the circular link to new head. -> Option CQuick Check:
Last node points to new head to keep circle [OK]
- Skipping update of last.next pointer
- Assuming newNode.next update is enough
- Incorrect traversal order after insertion
Ready to prove it?
Pass the test and this pattern moves unknown
