Insert at End of Circular Linked List in DSA Javascript - Time & Space Complexity
We want to understand how the time to insert a new node at the end of a circular 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.
function insertAtEnd(head, value) {
const newNode = { data: value, next: null };
if (!head) {
newNode.next = newNode;
return newNode;
}
let current = head;
while (current.next !== head) {
current = current.next;
}
current.next = newNode;
newNode.next = head;
return head;
}
This code inserts a new node with the given value at the end of a circular linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop that moves through nodes to find the last node. - How many times: It runs once for each node until it reaches the last node, so roughly
ntimes wherenis the number of nodes.
As the list grows, the loop must check more nodes to find the end.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to find the end |
| 100 | About 100 steps to find the end |
| 1000 | About 1000 steps to find the end |
Pattern observation: The number of steps grows roughly the same as the number of nodes, so it grows linearly.
Time Complexity: O(n)
This means the time to insert at the end grows directly with the size of the list.
[X] Wrong: "Inserting at the end is always fast and takes constant time."
[OK] Correct: Because we must find the last node by moving through the list, which takes longer as the list grows.
Understanding this helps you explain how linked list operations scale and shows you can analyze code beyond just writing it.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand empty circular linked list
An empty circular linked list has no nodes, so inserting the first node means it must point to itself to maintain circularity.Step 2: Insert new node and set its next pointer
The new node's next pointer is set to itself, making it the only node and forming a circular link.Final Answer:
The new node points to itself, becoming the only node in the list. -> Option AQuick Check:
Empty list insertion = node points to itself [OK]
- Setting next pointer to null instead of itself
- Assuming list remains empty after insertion
- Not maintaining circular link for single node
Solution
Step 1: Set new node's next to head
In a circular linked list, the last node points to the head, so newNode.next must point to head to maintain circularity.Step 2: Update last node's next to new node
The current last node's next pointer is updated to newNode, inserting it at the end.Final Answer:
newNode.next = head; lastNode.next = newNode; -> Option AQuick Check:
New node points to head, last node points to new node [OK]
- Setting newNode.next to null breaks circularity
- Swapping pointers incorrectly
- Not updating last node's next pointer
Solution
Step 1: Identify current list structure
The list currently has nodes 1, 2, 3 linked circularly, so last node 3 points back to 1.Step 2: Insert node with value 4 at end
New node 4's next points to head (1), and node 3's next points to node 4, extending the circle.Final Answer:
1 -> 2 -> 3 -> 4 -> (back to 1) -> Option BQuick Check:
New node added at end, circle maintained [OK]
- Placing new node at start instead of end
- Breaking circular link by pointing to null
- Incorrectly updating pointers causing broken list
function insertEnd(head, data) {
let newNode = { value: data, next: null };
if (!head) {
head = newNode;
newNode.next = head;
return head;
}
let temp = head;
while (temp.next !== head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
return head;
}Solution
Step 1: Check new node's next pointer
New node's next is initialized to null and never updated to point to head, breaking the circular link.Step 2: Verify circularity after insertion
Since temp.next points to newNode but newNode.next is null, the list is no longer circular.Final Answer:
New node's next is not set to head, breaking circularity. -> Option DQuick Check:
New node next must point to head [OK]
- Forgetting to link new node back to head
- Incorrect loop condition causing infinite loop
- Not handling empty list properly
Solution
Step 1: Find correct sorted position for new node
Traverse the list to find where 40 fits between nodes to keep ascending order.Step 2: Insert new node at found position and update pointers
Insert new node between nodes, update previous node's next to new node, and new node's next to next node, maintaining circularity.Final Answer:
Traverse to find correct position for 40, insert it there, update pointers to keep circularity. -> Option CQuick Check:
Sorted insert requires correct position and pointer updates [OK]
- Inserting at end without sorting
- Breaking circularity during insertion
- Not updating last node's next pointer correctly
Ready to prove it?
Pass the test and this pattern moves unknown
