26. Insert in Sorted Circular Linked List
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given a sorted circular linked list, insert a new node containing a given data value into the list so that it remains a sorted circular linked list.
📘 Examples
Example 1:
Input:
head = 1→2→4 (circular), data = 2
Output:
1→2→2→4 (circular)
Explanation:
We insert the new node with 2 after the second node.
Example 2:
Input:
head = 1→4→7→9 (circular), data = 5
Output:
1→4→5→7→9 (circular)
Explanation:
We insert the new node with 5 after the node containing 4.
🔒 Constraints
$
2 ≤ number of nodes ≤ 10^6$$
0 ≤ node->data ≤ 10^6$$
0 ≤ data ≤ 10^6$
✅ My Approach
Gap Detection
We traverse the circular list looking for the proper "gap" between two consecutive nodes where the new data fits, taking into account the wrap‑around point (largest to smallest).
Algorithm Steps:
Create the new node
nwithn->data = data.If
headisnull, maken->next = nand returnn.Use pointer
cur = head.Loop indefinitely:
Let
nxt = cur->next.Case 1: Normal gap: if
cur->data ≤ data ≤ nxt->data, insert betweencurandnxt.Case 2: Wrap‑around gap: if
cur->data > nxt->data(largest→smallest transition) and(data ≥ cur->data or data ≤ nxt->data), insert here.Case 3: Full cycle fallback: if
nxt == head, insert here.On insertion: set
cur->next = n,n->next = nxt.If
data < head->data, updatehead = nto maintain head as smallest.Break the loop.
Return
head.
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as in the worst case we traverse all nodes once to find the insertion point.
Expected Auxiliary Space Complexity: O(1), since we use only a fixed number of pointers.
🧠 Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Linear Traversal with Do‑While Loop
Algorithm Steps:
Create new node
n.If
headisnull, linknto itself and return.Set
cur = head.do-while loop until
curreturns tohead:Let
nxt = cur->next.If
(cur->data ≤ data ≤ nxt->data)or at wrap‑point(cur->data > nxt->data and (data ≥ cur->data or data ≤ nxt->data)), do:cur->next = n; n->next = nxt;If
data < head->datathenhead = n.Break.
cur = nxt.
Return
head.
✅ Why This Approach?
Uses a clear
do-whileto guarantee at least one full cycle.Cleaner separation of traversal and insertion logic.
📝 Complexity Analysis:
Time: 🔸 O(n) — one cycle maximum.
Space: 🟢 O(1).
🪜 3️⃣ Find Maximum Node Then Insert
Algorithm Steps:
Traverse to find
maxNodewheremaxNode->next == heador data decreases.Start from
maxNode->next(smallest), traverse until correct insert point(cur->data ≤ data ≤ cur->next->data).If none found by
maxNode, insert aftermaxNode.Link new node and update
headif needed.
✅ Why This Approach?
Explicitly handles the rotation point.
Two-phase traversal: find boundary then insert.
📝 Complexity Analysis:
Time: 🔸 O(n).
Space: 🟢 O(1).
🔃 4️⃣ Break, Insert, Reconnect
Algorithm Steps:
Find
tailnode (wheretail->next == head) and break the cycle (tail->next = nullptr).Perform standard sorted insert on linear list.
Reconnect
tailto the new head to restore circularity.Return the new head.
✅ Why This Works:
Leverages familiar linear list insert.
Easy to debug by isolating circular logic.
📝 Complexity Analysis:
Time: 🔸 O(n).
Space: 🟢 O(1).
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
▶️ Gap Detection (main)
🟢 O(n)
🟢 O(1)
Elegant, single pass handles all edge cases
Condition‑heavy logic
🔁 Do‑While Traversal
🔸 O(n)
🟢 O(1)
Guaranteed cycle, clear syntax
Slightly more verbose
🪜 Max Node First
🔸 O(n)
🟢 O(1)
Separates boundary detection from insertion
Two traversal phases
🔃 Break → Insert → Reconnect
🔸 O(n)
🟢 O(1)
Simplifies to linear insert + reconnect
Modifies structure temporarily
✅ Best Choice by Scenario
Scenario
Recommended Approach
🔁 Want concise, all‑in‑one detection/insert
🥇 Gap Detection
🔧 Prefer explicit loop control
🥈 Do‑While Traversal
🧪 Need clear boundary then insertion logic
🥉 Max Node First
🪛 Familiar with linear inserts
🔹 Break → Insert → Reconnect
🧑💻 Code (Java)
🐍 Code (Python)
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated