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
n
withn->data = data
.If
head
isnull
, maken->next = n
and returnn
.Use pointer
cur = head
.Loop indefinitely:
Let
nxt = cur->next
.Case 1: Normal gap: if
cur->data โค data โค nxt->data
, insert betweencur
andnxt
.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 = n
to 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