What if adding a new friend to a circle could break the whole group? Let's see how to avoid that!
Why Insert at Beginning of Circular Linked List in DSA Typescript?
Imagine you have a group of friends sitting in a circle, passing a ball around. Now, you want to add a new friend right at the start of the circle. Doing this by just pointing to the first friend is tricky because the circle loops back to the start, and you must make sure everyone still passes the ball correctly.
If you try to add the new friend by just changing the start pointer, the circle breaks. The last friend might still point to the old start, causing confusion or losing the new friend in the circle. Fixing this by hand means checking every friend to update their pointers, which is slow and easy to mess up.
Using the circular linked list insertion method, you can add the new friend at the beginning by carefully updating just a few pointers: the new friend's next points to the old start, and the last friend's next points to the new friend. This keeps the circle unbroken and the ball passing smooth.
let last = start; // To insert at beginning, find last node and update pointers manually while (last.next !== start) { last = last.next; } last.next = newNode; newNode.next = start; start = newNode;
function insertAtBeginning(newNode) {
if (!start) {
newNode.next = newNode;
start = newNode;
} else {
newNode.next = start;
let last = start;
while (last.next !== start) {
last = last.next;
}
last.next = newNode;
start = newNode;
}
}This method lets you quickly and safely add new elements at the start of a circular list, keeping the loop intact and operations efficient.
Think of a music playlist that loops songs endlessly. Adding a new song at the start without breaking the loop means the playlist plays smoothly without interruption.
Manual pointer updates in circular lists are tricky and error-prone.
Proper insertion updates only necessary pointers to keep the circle intact.
This operation enables efficient and safe additions at the start of circular lists.
Practice
Solution
Step 1: Understand the circular linked list structure
A circular linked list has its last node pointing 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 DQuick Check:
New head points to old head, circle intact [OK]
- Forgetting to update last node's next pointer
- Breaking the circular link by not connecting last node
- Replacing the last node instead of adding new head
Solution
Step 1: Link new node to current head
newNode.next = head sets the new node's next pointer to the current head.Step 2: Update last node's next pointer and head
last.next = newNode updates the last node to point to the new node, and head = newNode sets the new node as the head.Final Answer:
newNode.next = head; last.next = newNode; head = newNode; -> Option AQuick Check:
Correct pointer updates maintain circular list [OK]
- Setting newNode.next to null breaks circularity
- Not updating last.next to newNode
- Assigning head incorrectly before linking nodes
class Node {
data: number;
next: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
}
}
let head = new Node(10);
let second = new Node(20);
let third = new Node(30);
head.next = second;
second.next = third;
third.next = head; // circular link
let newNode = new Node(5);
// Insert at beginning
newNode.next = head;
third.next = newNode;
head = newNode;
// Print list
let temp = head;
do {
console.log(temp.data);
temp = temp.next!;
} while (temp !== head);Solution
Step 1: Understand the initial circular list
Initial list is 10 -> 20 -> 30 -> back to 10 (circular).Step 2: Insert new node with data 5 at beginning
newNode.next = head points 5 to 10; third.next = newNode points 30 to 5; head = newNode updates head to 5.Step 3: Print traversal
Starting at head (5), traversal prints 5, 10, 20, 30, then stops as it loops back to 5.Final Answer:
5 -> 10 -> 20 -> 30 (circular) -> Option AQuick Check:
New head 5 leads list, circle intact [OK]
- Printing null at end instead of circular loop
- Not updating last node's next pointer
- Confusing order of nodes after insertion
newNode.next = head; head = newNode; last.next = newNode;
Solution
Step 1: Analyze pointer updates order
newNode.next = head sets new node's next to old head correctly.Step 2: Verify the updates
head = newNode updates head to new node. Then last.next = newNode correctly sets the last node's next to the new head since 'last' references the unchanged last node.Step 3: Confirm circularity
The final structure is newHead -> oldHead -> ... -> last -> newHead, fully circular with no breaks.Final Answer:
No error; code correctly inserts at beginning. -> Option BQuick Check:
last reference stable, circle intact [OK]
- Assuming changing head before last.next breaks circle
- Not updating last.next at all
- Pointing newNode.next incorrectly
Solution
Step 1: Link new node to current head
newNode.next = head points the new node (5) to the current head (15).Step 2: Update last node's next pointer
last.next = newNode updates the last node (35) to point to the new node (5), maintaining circularity.Step 3: Update head pointer
head = newNode sets the new node as the head of the list.Final Answer:
Set newNode.next = head; last.next = newNode; head = newNode; -> Option CQuick Check:
New head points to old head; last points to new head [OK]
- Updating pointers in wrong order
- Pointing newNode.next to last instead of head
- Not updating last.next to newNode
Ready to prove it?
Pass the test and this pattern moves unknown
