Create a Circular Singly Linked List in DSA Java - 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.
class Node {
int data;
Node next;
Node(int data) { this.data = data; this.next = null; }
}
class CircularLinkedList {
Node head = null;
void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
newNode.next = head;
} else {
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
}
}
}
This code adds nodes to a circular singly linked list, linking the last node back to the head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that finds the last node before adding a new node.
- How many times: It runs once for each existing node in the list before the new node is added.
Each time we add a node, we walk through the list to find the last node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to add the last node |
| 100 | About 100 steps to add the last node |
| 1000 | About 1000 steps to add the last node |
Pattern observation: The work to add each new node grows linearly with the list size.
Time Complexity: O(n)
This means adding a node takes longer as the list grows, roughly proportional to the number of nodes already in the list.
[X] Wrong: "Adding a node always takes the same time, no matter how big the list is."
[OK] Correct: Because the code searches through all existing nodes to find the last one, the time grows with the list size.
Understanding how adding nodes scales helps you explain trade-offs in linked list designs and shows you can analyze code efficiency clearly.
"What if we kept a reference to the last node (tail) in the list? How would the time complexity of adding a node change?"
Practice
Solution
Step 1: Understand the structure of a regular singly linked list
A regular singly linked list has nodes where each node points to the next node, and the last node points to null.Step 2: Understand the structure of a circular singly linked list
In a circular singly linked list, the last node points back to the first node, forming a circle.Final Answer:
In a circular singly linked list, the last node points back to the first node. -> Option CQuick Check:
Last node points to first node = B [OK]
- Thinking circular means nodes have two pointers
- Confusing circular list with doubly linked list
- Assuming last node points to null
Solution
Step 1: Understand node creation for circular singly linked list
When creating a single node in a circular singly linked list, its next pointer should point to itself to form a circle.Step 2: Check each option
Node newNode = new Node(data); newNode.next = newNode; sets newNode.next = newNode, which correctly points the node to itself. Other options set next to null or do not initialize properly.Final Answer:
Node newNode = new Node(data); newNode.next = newNode; -> Option DQuick Check:
Node points to itself when alone = A [OK]
- Setting next to null for single node
- Not initializing next pointer
- Confusing syntax for node creation
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class Test {
public static void main(String[] args) {
Node head = new Node(10);
Node second = new Node(20);
head.next = second;
second.next = head;
System.out.print(head.data + " -> " + head.next.data + " -> " + head.next.next.data);
}
}Solution
Step 1: Trace the node connections
head.data = 10, head.next = second (data 20), second.next = head (data 10), forming a circle.Step 2: Evaluate the print statement
head.data = 10, head.next.data = 20, head.next.next.data = second.next.data = head.data = 10.Final Answer:
10 -> 20 -> 10 -> Option AQuick Check:
Circle repeats first node data = 10 -> 20 -> 10 [OK]
- Assuming next.next is null
- Confusing node data values
- Expecting compilation error
public void insertEnd(Node head, int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
newNode.next = head;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
}
}Solution
Step 1: Understand circular list traversal
In a circular singly linked list, the last node's next points to head, not null.Step 2: Analyze the while loop condition
The loop checks temp.next != null, which never happens in circular list; it should check temp.next != head to find the last node.Final Answer:
The while loop condition should check for temp.next != head instead of null. -> Option AQuick Check:
Loop must stop at last node pointing to head = A [OK]
- Using null to detect end in circular list
- Not updating newNode.next correctly
- Ignoring head update when list is empty
Solution
Step 1: Insert nodes at end in given order
Insert 5 first, then 10 at end, then 15 at end, preserving order: 5, 10, 15.Step 2: Set last node's next to head
After insertion, last node (15) points back to head (5), making the list circular.Final Answer:
5 -> 10 -> 15 -> 5 -> Option BQuick Check:
Order preserved, last points to first = C [OK]
- Assuming last node points to null
- Reversing insertion order
- Mixing node data order
Ready to prove it?
Pass the test and this pattern moves unknown
