Insert into a Sorted Circular Linked List Problem
Description
LeetCode Problem 708.
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:
- The number of nodes in the list is in the range
[0, 5 * 10^4]. -10^6 <= Node.val, insertVal <= 10^6
Solution
Approach: One Pass Around the Ring
If the list is empty, create a node that points to itself and return it.
Otherwise, walk around the ring with two pointers, prev and curr = prev.next, starting at head. Going around a sorted circular list, the values never decrease, except at one place where they drop from the largest value back to the smallest. The new value can go between prev and curr in two situations:
prev.val <= insertVal <= curr.val: it fits inside an ascending run.prev.val > curr.val(this is the drop) andinsertValis a new maximum (>= prev.val) or a new minimum (<= curr.val). It belongs right at the drop.
Stop at the first pair that works and link the new node between prev and curr.
The tricky case is a list where every value is the same, like [3,3,3] with insertVal = 5. There is no drop, and no pair satisfies either rule. The loop then goes all the way around and stops when curr is back at head. Any place is fine in this list, so we insert there, between the last node and head. Always return the original head, not the new node.
Complexity: O(n) time and O(1) extra space.
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, next=None):
self.val = val
self.next = next
"""
class Solution:
def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
node = Node(insertVal)
if head is None:
node.next = node # a single node points to itself
return node
prev, curr = head, head.next
while curr is not head:
if prev.val <= insertVal <= curr.val:
break # fits inside an ascending run
if prev.val > curr.val and (insertVal >= prev.val or insertVal <= curr.val):
break # at the drop from the largest to the smallest value: a new max or min goes here
prev, curr = curr, curr.next
# If we went all the way around (all values equal), any place works: insert before head.
prev.next = node
node.next = curr
return head