Insert at End of Circular Linked List in DSA Java - Time & Space Complexity
We want to understand how the time to insert a node at the end of a circular linked list changes as the list grows.
How does the number of steps needed grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node next;
Node(int data) { this.data = data; this.next = null; }
}
Node insertEnd(Node head, int value) {
Node newNode = new Node(value);
if (head == null) {
newNode.next = newNode;
return newNode;
}
Node temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
return head;
}
This code inserts a new node at the end of a circular linked list by traversing the list to find the last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through each node until it finds the last node.
- How many times: It runs once for each node in the list until the last node is found, so roughly n times for a list of size n.
As the list size grows, the number of steps to find the last node grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach the end |
| 100 | About 100 steps to reach the end |
| 1000 | About 1000 steps to reach the end |
Pattern observation: The steps increase directly with the number of nodes, so doubling the list size roughly doubles the work.
Time Complexity: O(n)
This means the time to insert at the end grows linearly with the size of the list.
[X] Wrong: "Inserting at the end is always constant time because we just add the node."
[OK] Correct: We must first find the last node by moving through the list, which takes time proportional to the list size.
Understanding this helps you explain how linked list operations scale and shows you can analyze code beyond just reading it.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand circular linked list empty state
When the list is empty, there is no node, so the new node must point to itself to form a circle.Step 2: Insert new node and update pointers
The new node's next pointer is set to itself, making it the only node and the head of the list.Final Answer:
The new node points to itself, becoming the only node in the list. -> Option BQuick Check:
Empty list insertion = node points to itself [OK]
- Setting new node's next to null
- Not updating head pointer
- Assuming list remains empty
Solution
Step 1: Set new node's next to head
In a circular linked list, the last node points to the head, so newNode.next = head is correct.Step 2: Update tail's next and tail pointer
Tail's next should point to newNode, and tail should be updated to newNode to maintain the list.Final Answer:
newNode.next = head; tail.next = newNode; tail = newNode; -> Option AQuick Check:
New node points to head, tail updated [OK]
- Setting newNode.next to null
- Updating head.next instead of tail.next
- Not updating tail pointer
Node head = new Node(10); Node second = new Node(20); Node third = new Node(30); head.next = second; second.next = third; third.next = head; Node newNode = new Node(40); Node tail = third; newNode.next = head; tail.next = newNode; tail = newNode;
Solution
Step 1: Understand initial list and insertion
Initial list is 10 -> 20 -> 30 -> back to 10. Inserting 40 at end means 30's next points to 40, and 40 points to head (10).Step 2: Update tail and next pointers
After insertion, tail is updated to 40, and list becomes 10 -> 20 -> 30 -> 40 -> back to 10.Final Answer:
10 -> 20 -> 30 -> 40 -> back to 10 -> Option CQuick Check:
New node added at end, circular link maintained [OK]
- Not updating tail.next to new node
- Not updating tail pointer
- Breaking circular link
Node newNode = new Node(50); newNode.next = null; tail.next = newNode; tail = newNode; tail.next = head;
Solution
Step 1: Check newNode.next assignment
Setting newNode.next to null breaks the circular property because last node must point to head.Step 2: Verify tail.next and tail updates
tail.next is set to newNode and tail updated correctly, but newNode.next should point to head, not null.Final Answer:
Setting newNode.next to null breaks the circular link. -> Option AQuick Check:
newNode.next must point to head, not null [OK]
- Setting newNode.next to null
- Not maintaining circular link
- Confusing tail and head pointers
Node head = new Node(5); Node second = new Node(15); Node third = new Node(25); head.next = second; second.next = third; third.next = head; Node newNode = new Node(35); Node tail = third; // Insert newNode at end // Print list starting from head
Solution
Step 1: Insert newNode correctly at end
Set newNode.next to head, update tail.next to newNode, and update tail to newNode to maintain circular list.Step 2: Print list starting from head using do-while
Use a do-while loop starting at head, printing each node until we return to head, ensuring all nodes are printed once.Final Answer:
newNode.next = head; tail.next = newNode; tail = newNode; followed by do-while print from head -> Option DQuick Check:
Insert at end + do-while print from head [OK]
- Setting newNode.next to null
- Using while loop with null condition
- Starting print from tail instead of head
Ready to prove it?
Pass the test and this pattern moves unknown
