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++)
class Solution {
public:
Node* sortedInsert(Node* head, int data) {
Node* n = new Node(data);
if (!head) {
n->next = n;
return n;
}
Node* cur = head;
while (true) {
Node* nxt = cur->next;
if ((cur->data <= data && data <= nxt->data) ||
(cur->data > nxt->data && (data >= cur->data || data <= nxt->data)) ||
nxt == head) {
cur->next = n;
n->next = nxt;
if (data < head->data) head = n;
break;
}
cur = nxt;
}
return head;
}
};🧑💻 Code (Java)
class Solution {
public Node sortedInsert(Node head, int data) {
Node n = new Node(data);
if (head == null) {
n.next = n;
return n;
}
Node cur = head;
while (true) {
if ((cur.data <= data && cur.next.data >= data) ||
(cur.data > cur.next.data && (data >= cur.data || data <= cur.next.data)) ||
cur.next == head) {
n.next = cur.next;
cur.next = n;
if (data < head.data) head = n;
break;
}
cur = cur.next;
}
return head;
}
}🐍 Code (Python)
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def sortedInsert(self, head, data):
n = Node(data)
if not head:
n.next = n
return n
cur = head
while True:
if (cur.data <= data <= cur.next.data or
cur.data > cur.next.data and (data >= cur.data or data <= cur.next.data) or
cur.next == head):
n.next = cur.next
cur.next = n
if data < head.data:
head = n
break
cur = cur.next
return head🧠 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