Insert a value into a sorted circular linked list
A circular linked list holds integers. Going round the circle once from its smallest value, the values never decrease; then the circle wraps from the largest value back to the smallest.
You get the circle opened up at some node, which need not hold the smallest value: a linked list whose first node is that node and whose last node is the one that, in the circle, links back to the first. Insert a new node holding value at a place that keeps the circle sorted. Return the circle opened at the same starting node, so the original first node stays first.
If the circle is empty, return a list with just the new node. If several places keep the circle sorted (for example when values repeat), any of them is accepted.
Examples
Constraints
- The circle has between 0 and 100,000 nodes
- Values and value are between -1,000,000,000 and 1,000,000,000
- The values are sorted round the circle as described
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.