Given a Circular Linked List node, which is sorted in non-descending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list and may not necessarily be the smallest value in the circular list.
If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.
If the list is empty (i.e., the given node is null), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the originally given node.
Example 1:

Input: head = [3,4,1], insertVal = 2 Output: [3,4,1,2] Explanation: In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.![]()
Example 2:
Input: head = [], insertVal = 1
Output: [1]
Explanation: The list is empty (given head is null). We create a new single circular list and return the reference to that single node.
Example 3:
Input: head = [1], insertVal = 0 Output: [1,0]
Constraints:
[0, 5 * 104].-106 <= Node.val, insertVal <= 106Problem Overview: You receive a reference to any node in a sorted circular linked list and a value to insert. The task is to insert a new node so the list remains sorted while preserving the circular structure.
This problem tests careful pointer traversal in a linked list where the head is not guaranteed to be the smallest element. Because the list is circular, traversal can start from any node and must detect the correct insertion window.
Approach 1: Linear Scan with Sorted Insertion (O(n) time, O(1) space)
Traverse the circular list using two pointers: curr and next. For each pair of nodes, check whether the new value belongs between them. The typical condition is curr.val ≤ insertVal ≤ next.val. Another critical case occurs at the rotation point where the maximum value connects back to the minimum (curr.val > next.val). If the value is larger than the current maximum or smaller than the minimum, insert at this boundary.
If you complete a full cycle without finding a suitable spot, all nodes contain the same value or the value fits anywhere. Insert the new node after the current node. This approach performs a single pass through the list and only updates pointers, giving O(n) time and O(1) extra space.
Approach 2: Find Rotation Pivot then Insert (O(n) time, O(1) space)
Another way to reason about the list is to first locate the pivot where the sorted order breaks (curr.val > next.val). This point separates the maximum and minimum elements. After identifying the pivot, treat next as the logical head of a sorted list and insert the new value using a standard sorted insertion scan.
This approach separates the problem into two clearer phases: identifying the boundary and inserting in order. The time complexity remains O(n) with O(1) space because the list is scanned at most once or twice. It can be easier to reason about during debugging.
Approach 3: Convert to Array, Insert, Rebuild (O(n) time, O(n) space)
A brute-force strategy converts the circular list into an array by traversing until you return to the starting node. Insert the value into the sorted array using binary search or linear insertion, then rebuild the circular linked list.
This method simplifies reasoning but wastes memory and loses the advantage of pointer-based operations. It runs in O(n) time and O(n) space. Interviewers rarely expect this solution but it can help verify logic when first approaching circular structures.
Recommended for interviews: The linear scan insertion approach is the expected solution. It demonstrates that you understand circular traversal, boundary conditions, and pointer manipulation in circular linked lists. Explaining the rotation-point case (max → min) clearly usually matters more than the code itself.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Scan with Sorted Insertion | O(n) | O(1) | General case and expected interview solution |
| Find Rotation Pivot then Insert | O(n) | O(1) | When separating pivot detection makes reasoning clearer |
| Convert to Array and Rebuild | O(n) | O(n) | Conceptual or debugging approach when pointer logic is difficult |
INSERT INTO CIRCULAR LINKED LIST | LEETCODE # 708 | PYTHON SOLUTION • Cracking FAANG • 10,138 views views
Watch 9 more video solutions →Practice Insert into a Sorted Circular Linked List with our built-in code editor and test cases.
Practice on FleetCode