Create a Circular Singly Linked List in DSA C - Time & Space Complexity
When creating a circular singly linked list, we want to know how the time to build it grows as we add more nodes.
We ask: How does the work increase when the list gets longer?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
struct Node* next;
};
struct Node* createCircularList(int arr[], int n) {
if (n == 0) return NULL;
struct Node* head = malloc(sizeof(struct Node));
head->data = arr[0];
struct Node* temp = head;
for (int i = 1; i < n; i++) {
temp->next = malloc(sizeof(struct Node));
temp = temp->next;
temp->data = arr[i];
}
temp->next = head; // Make it circular
return head;
}
This code creates a circular singly linked list from an array of integers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that creates each new node and links it.
- How many times: It runs once for each element after the first, so (n - 1) times.
As the number of elements increases, the loop runs more times, adding one node each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 node creations and links |
| 100 | About 99 node creations and links |
| 1000 | About 999 node creations and links |
Pattern observation: The work grows directly with the number of elements; doubling n roughly doubles the work.
Time Complexity: O(n)
This means the time to create the list grows linearly with the number of nodes.
[X] Wrong: "Creating a circular list takes more time than a normal list because of the circular link."
[OK] Correct: Adding the final link to make the list circular is just one extra step, so it does not change the overall linear time.
Understanding how list creation scales helps you explain your code clearly and shows you grasp fundamental data structure costs.
"What if we added a step to find the last node each time before linking a new node? How would the time complexity change?"
Practice
circular singly linked list compared to a regular singly linked list?Solution
Step 1: Understand the pointer structure in circular singly linked list
In a circular singly linked list, the last node's next pointer points back to the first node, creating a loop.Step 2: Compare with regular singly linked list
Regular singly linked lists end with a null pointer, while circular ones loop back to the start.Final Answer:
The last node points back to the first node, forming a circle. -> Option AQuick Check:
Circular list last node points to head = C [OK]
- Thinking circular list ends with null pointer
- Confusing circular singly linked list with doubly linked list
- Assuming nodes have two pointers in singly linked list
Solution
Step 1: Understand initialization of single node circular list
For a circular singly linked list with one node, the node's next pointer must point to itself to form a circle.Step 2: Check each option
node->next = node; sets node->next = node, correctly creating a self-loop. Others either set null or incorrect pointers.Final Answer:
node->next = node; -> Option CQuick Check:
Single node points to itself = A [OK]
- Setting next pointer to NULL instead of self
- Using uninitialized pointers
- Confusing NULL with 0 in pointer context
typedef struct Node {
int data;
struct Node* next;
} Node;
int main() {
Node n1 = {1, NULL};
Node n2 = {2, NULL};
Node n3 = {3, NULL};
n1.next = &n2;
n2.next = &n3;
n3.next = &n1; // circular link
Node* temp = &n1;
int count = 0;
while (count < 5) {
printf("%d ", temp->data);
temp = temp->next;
count++;
}
return 0;
}Solution
Step 1: Trace the linked list nodes and their next pointers
n1 points to n2, n2 points to n3, n3 points back to n1, forming a circle: 1 -> 2 -> 3 -> 1 ...Step 2: Follow the print loop for 5 iterations
Print order: 1 (n1), 2 (n2), 3 (n3), 1 (n1 again), 2 (n2 again).Final Answer:
1 2 3 1 2 -> Option DQuick Check:
Loop prints nodes in circle: 1 2 3 1 2 [OK]
- Stopping after 3 nodes instead of 5
- Not following circular link back to first node
- Confusing node data values
typedef struct Node {
int data;
struct Node* next;
} Node;
int main() {
Node* n1 = malloc(sizeof(Node));
Node* n2 = malloc(sizeof(Node));
n1->data = 10;
n2->data = 20;
n1->next = n2;
n2->next = NULL; // mistake here
return 0;
}Solution
Step 1: Identify the circular link requirement
In a circular singly linked list, the last node's next pointer must point back to the first node.Step 2: Check the code's next pointers
n1->next points to n2, but n2->next is NULL, breaking the circle. It should point to n1.Final Answer:
n2->next should point to n1 to make the list circular. -> Option BQuick Check:
Last node next points to head = D [OK]
- Setting last node next to NULL instead of head
- Forgetting to allocate memory for nodes
- Mixing up which node points where
Solution
Step 1: Create new node and link it to head
The new node's next pointer must point to the head to keep the circle intact.Step 2: Find the last node and update its next pointer
Traverse the list to find the last node (which points to head), then set last_node->next to new_node.Final Answer:
Create new node; set new_node->next = head; find last node; set last_node->next = new_node; -> Option AQuick Check:
New node next = head, last node next = new node = A [OK]
- Setting new node next to NULL breaks circularity
- Not updating last node's next pointer
- Confusing head and last node pointers
Ready to prove it?
Pass the test and this pattern moves unknown
