Insert at Beginning of Circular Linked List in DSA C - Time & Space Complexity
We want to understand how long it takes to add a new node at the start of a circular linked list.
How does the time needed change as the list grows?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
*head = newNode;
}
This code adds a new node at the start of a circular linked list, updating links to keep the circle intact.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that finds the last node in the list.
- How many times: It runs once for each node until it reaches the last node, so it runs n times if the list has n nodes.
As the list gets bigger, the loop runs more times to find the last node.
| 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 O(1) because we just change the head pointer."
[OK] Correct: In a circular linked list, we must also update the last node's next pointer, which requires finding the last node by traversing the list, taking O(n) time.
Understanding this helps you explain how linked list operations can vary in cost depending on the structure, showing your grasp of data structure details.
"What if we kept a pointer to the last node? How would the time complexity of inserting at the beginning change?"
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 BQuick 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
Set new_node->next to head so new node points to old first node.Step 2: Update last node's next pointer and head
Set last->next to new_node to maintain circular link, then update head to new_node.Final Answer:
last->next = new_node; new_node->next = head; head = new_node; -> Option CQuick Check:
New node links head and last points to new node [OK]
- Setting new_node->next to NULL breaks circular link
- Updating head before linking last node
- Confusing order of pointer assignments
struct Node {
int data;
struct Node* next;
};
// Existing list: 20 -> 30 -> 40 -> back to 20
struct Node* head = node20;
struct Node* last = node40;
// Insert 10 at beginning
struct Node* new_node = malloc(sizeof(struct Node));
new_node->data = 10;
new_node->next = head;
last->next = new_node;
head = new_node;
// Print list starting from head (print 4 nodes)Solution
Step 1: Insert new node at beginning
New node with data 10 points to old head (20), last node (40) points to new node, head updated to new node.Step 2: Print nodes starting from new head
Printing 4 nodes from head: 10, 20, 30, 40, then back to 10.Final Answer:
10 -> 20 -> 30 -> 40 -> back to 10 -> Option AQuick Check:
New head 10 followed by old nodes in order [OK]
- Not updating last->next to new node
- Printing old head as start
- Mixing node order after insertion
void insertAtBeginning(struct Node** head, int data) {
struct Node* new_node = malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = *head;
*head = new_node;
}Solution
Step 1: Check pointer updates
The code updates head to new node and new node points to old head, but does not update last node's next pointer.Step 2: Understand circular link maintenance
In circular linked list, last node must point to new head to keep circle intact; missing here causes broken link.Final Answer:
The last node's next pointer is not updated to new head, breaking circular link. -> Option DQuick Check:
Last node must point to new head [OK]
- Ignoring last node pointer update
- Assuming head update is enough
- Not handling empty list case
Solution
Step 1: Link new node between last and head
Set last->next to new node, then new_node->next to old head to maintain circle.Step 2: Update head pointer
Set head to new node so it becomes the first node in the list.Final Answer:
Create new node with 1, set last->next = new_node, new_node->next = head, head = new_node. -> Option AQuick Check:
Last points new node, new node points old head, head updated [OK]
- Setting new_node->next to NULL breaks circle
- Updating head before linking last node
- Confusing pointer order causing broken links
Ready to prove it?
Pass the test and this pattern moves unknown
