Create a Circular Singly Linked List in DSA C++ - Time & Space Complexity
We want to understand how the time needed to create a circular singly linked list changes as we add more nodes.
How does the work grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
Node* next;
};
Node* createCircularList(int n) {
if (n == 0) return nullptr;
Node* head = new Node{1, nullptr};
Node* curr = head;
for (int i = 2; i <= n; i++) {
curr->next = new Node{i, nullptr};
curr = curr->next;
}
curr->next = head; // Make it circular
return head;
}
This code creates a circular singly linked list with n nodes, linking the last node back to the first.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that creates and links each new node.
- How many times: It runs (n - 1) times, once for each node after the first.
Each new node requires one step to create and link, so the total steps grow directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps to add nodes + 1 step to link last to first = 10 |
| 100 | About 99 steps + 1 linking step = 100 |
| 1000 | About 999 steps + 1 linking step = 1000 |
Pattern observation: The work grows evenly as the list size grows; doubling n roughly doubles the work.
Time Complexity: O(n)
This means the time to create the list grows in a straight line with the number of nodes.
[X] Wrong: "Creating a circular list is faster than a normal list because of the circle link."
[OK] Correct: The extra step to link the last node back to the head is just one operation and does not reduce the overall linear growth of creating all nodes.
Understanding how list creation scales helps you explain your code clearly and shows you know how data grows in memory.
"What if we added nodes at the end one by one instead of all at once? How would the time complexity change?"
Practice
circular singly linked list compared to a regular singly linked list?Solution
Step 1: Understand the structure of a 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 other linked lists
Unlike doubly linked lists (which have two pointers) or arrays, circular singly linked lists have a single pointer per node and form a circle.Final Answer:
The last node points back to the first node, forming a circle. -> Option DQuick Check:
Circular list = last node points to first node [OK]
- Thinking circular means two pointers per node
- Confusing with doubly linked list
- Assuming it uses arrays internally
Solution
Step 1: Identify the correct node structure for singly linked list
A singly linked list node has data and a pointer to the next node. The pointer type must beNode*.Step 2: Check each option
struct Node { int data; Node* next; }; // Correct declaration correctly declaresNode* next. struct Node { int data; Node* prev; Node* next; }; // Doubly linked list node has an extraprevpointer (doubly linked list). struct Node { int data; int next; }; // Incorrect pointer type usesint nextwhich is wrong type. struct Node { int data; Node next; }; // Incorrect pointer usage usesNode nextwhich is not a pointer.Final Answer:
struct Node { int data; Node* next; }; -> Option CQuick Check:
Single pointer to next node = correct node structure [OK]
- Using non-pointer type for next
- Adding prev pointer for singly linked list
- Declaring next as object instead of pointer
struct Node {
int data;
Node* next;
};
int main() {
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
head->next = second;
second->next = third;
third->next = head; // circular link
Node* temp = head;
for (int i = 0; i < 5; i++) {
std::cout << temp->data << " ";
temp = temp->next;
}
return 0;
}Solution
Step 1: Understand the circular link setup
The last node (third) points back to head, so the list loops: 1 -> 2 -> 3 -> 1 -> ...Step 2: Trace the print loop for 5 iterations
Starting at head (1), print 1, move to 2, print 2, move to 3, print 3, move to 1, print 1, move to 2, print 2.Final Answer:
1 2 3 1 2 -> Option AQuick Check:
Loop prints nodes in circle: 1 2 3 1 2 [OK]
- Assuming list ends after 3 nodes
- Printing wrong data due to pointer errors
- Confusing circular with linear list output
struct Node {
int data;
Node* next;
};
int main() {
Node* head = new Node{10, nullptr};
head->next = nullptr; // Intended to make circular
return 0;
}Solution
Step 1: Check how circular link is formed
For a circular singly linked list with one node, the node's next pointer must point to itself.Step 2: Identify the mistake in the code
The code setshead->next = nullptr, which breaks the circular property.Final Answer:
head->next should point to head, not nullptr, to form a circular list. -> Option BQuick Check:
One node circular list: next points to itself [OK]
- Setting next to nullptr instead of self
- Confusing circular with linear list
- Ignoring memory management issues (not relevant here)
Solution
Step 1: Understand insertion at end in circular singly linked list
To insert at end, find last node (whose next points to head), then link new node to head and last node to new node.Step 2: Analyze each option
Node* newNode = new Node{4, head}; Node* temp = head; while (temp->next != head) temp = temp->next; temp->next = newNode; correctly creates new node pointing to head, finds last node by looping untiltemp->next == head, then sets last node's next to new node. Node* newNode = new Node{4, nullptr}; Node* temp = head; while (temp->next != nullptr) temp = temp->next; temp->next = newNode; newNode->next = head; incorrectly checks for nullptr which does not exist in circular list. Node* newNode = new Node{4, head}; head->next = newNode; incorrectly sets head->next to new node, breaking list. Node* newNode = new Node{4, nullptr}; newNode->next = head; head = newNode; changes head pointer incorrectly and does not update last node's next.Final Answer:
Node* newNode = new Node{4, head}; Node* temp = head; while (temp->next != head) temp = temp->next; temp->next = newNode; -> Option AQuick Check:
Find last node by next == head, link new node to head, last node to new node [OK]
- Checking for nullptr in circular list
- Not updating last node's next pointer
- Incorrectly changing head pointer
Ready to prove it?
Pass the test and this pattern moves unknown
