Question:
Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list.
public static void insertCyclicList(Node head, int data) {
Node node = new Node(data);
if (head == null) {
node.next = node;
}
Node current = head;
Node prev = null;
do {
prev = current;
current = current.next;
if (data <= current.val && data >= prev.val) break;
if (prev.val > current.val && (prev.val < data || current.val > data)) break;
} while (head != current);
prev.next = node;
node.next = current;
}