Insert into a Sorted Circular Linked List

Insert a value into a sorted circular list at the correct position, handling wrap-around and uniform-value cases.

Medium
Time O(n) Space O(1)
LeetCode
4 min read
linked-list

Problem#

Given a node in a sorted circular linked list (any node), insert a new value preserving sortedness. Return any node of the modified list (must include the new node).

Examples#

  • head = [3,4,1] with cycle, insertVal = 2[3,4,1,2]
  • head = null, insertVal = 1[1] (self-loop)

Constraints#

  • 0 <= n <= 5 * 10^4.

Hints#

Hint 1
Three insertion conditions: between two sorted neighbors; at the wrap-around between max and min; uniform-value lap.

Approach#

Walk one full lap. Insert when:

  1. curr <= val <= next (normal slot)
  2. curr > next (the wrap-around) and val >= curr || val <= next (val belongs at the boundary)
  3. We completed a full lap with no insertion (every value equals the others) — insert anywhere.

Solution#

Insert Into Sorted Circular Linked List
class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* node = new Node(insertVal);
if (!head) { node->next = node; return node; }
Node *curr = head;
do {
Node* nxt = curr->next;
if ((curr->val <= insertVal && insertVal <= nxt->val) ||
(curr->val > nxt->val && (insertVal >= curr->val || insertVal <= nxt->val))) {
curr->next = node;
node->next = nxt;
return head;
}
curr = nxt;
} while (curr != head);
// Uniform-value list — insert anywhere.
node->next = head->next;
head->next = node;
return head;
}
};

Editorial#

Three cases is exactly right. The wrap-around predicate handles values smaller than the minimum and larger than the maximum (they both belong at the boundary). The fallback after the do-while handles the all-equal degenerate case — we walked the entire cycle and never found a “between two values” slot.

Complexity#

  • Time: O(n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.