Insert at End of Circular Linked List in DSA Typescript - Time & Space Complexity
We want to know 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 grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function insertAtEnd(head: Node | null, value: number): Node {
const newNode = new Node(value);
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 at the end of a circular linked list by traversing the list to find the last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through each node to find the last node.
- How many times: It runs once for each node until it reaches the last node, so it runs n-1 times if there are n nodes.
As the list grows, the time to find the last node grows linearly because we check each node once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps to find the end |
| 100 | About 99 steps to find the end |
| 1000 | About 999 steps to find the end |
Pattern observation: The steps increase directly with the number of nodes, so doubling nodes doubles the steps.
Time Complexity: O(n)
This means the time to insert grows in direct proportion to the number of nodes in the list.
[X] Wrong: "Inserting at the end is always quick because we just add a node."
[OK] Correct: We must first find the last node by checking each node, which takes time proportional to the list size.
Understanding this helps you explain how linked list operations scale and shows you can analyze simple loops clearly.
"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 the head of the list.Final Answer:
The new node points to itself, becoming the only node in the list. -> Option BQuick Check:
Empty list insertion = node points to itself [OK]
- Setting next pointer to null instead of itself
- Assuming list remains empty after insertion
- Not updating head pointer to new 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.Step 2: Update tail's next and tail pointer
Tail's next is updated to newNode, and tail pointer moves to newNode to mark new end.Final Answer:
newNode.next = head; tail.next = newNode; tail = newNode; -> Option CQuick Check:
New node points to head, tail updated [OK]
- Setting newNode.next to null breaks circularity
- Updating head.next instead of tail.next
- Not moving tail pointer to new node
Solution
Step 1: Understand circular list structure
Original list loops from 30 back to 10, so last node 30 points to head 10.Step 2: Insert 40 at end and update pointers
New node 40 points to head 10, old tail 30 points to 40, maintaining circularity.Final Answer:
10 -> 20 -> 30 -> 40 -> 10 -> Option AQuick Check:
New node added before head, tail points to new node [OK]
- Adding null at end breaks circularity
- Inserting new node at head instead of tail
- Incorrect pointer updates causing loops
function insertEnd(tail: Node | null, data: number): Node {
const newNode = new Node(data);
if (!tail) {
newNode.next = null;
return newNode;
}
newNode.next = tail.next;
tail.next = newNode;
return newNode;
}Solution
Step 1: Check empty list insertion
When tail is null (empty list), newNode.next should point to itself to maintain circularity, not null.Step 2: Verify pointer updates for non-empty list
For non-empty list, newNode.next = tail.next (head), tail.next = newNode is correct.Final Answer:
newNode.next should point to itself when list is empty -> Option AQuick Check:
Empty list node points to itself [OK]
- Setting newNode.next to null breaks circularity
- Returning newNode instead of updating tail reference
- Not updating tail.next pointer
Solution
Step 1: Insert 35 at end
After inserting 35, list is 5 -> 15 -> 25 -> 35 -> back to 5.Step 2: Insert 2 at beginning
Insert at beginning means new node 2 points to current head 5, tail's next updated to 2, making 2 the new head.Step 3: Final circular list
The list is 2 -> 5 -> 15 -> 25 -> 35 -> back to 2, maintaining circularity.Final Answer:
2 -> 5 -> 15 -> 25 -> 35 -> 2 -> Option DQuick Check:
New head inserted, tail points to new head [OK]
- Not updating tail.next after inserting at beginning
- Inserting new node at end but not updating tail
- Breaking circular link by pointing to wrong node
Ready to prove it?
Pass the test and this pattern moves unknown
