Insert into a Sorted Circular Linked List
Given a reference to any node in a non-decreasing sorted circular singly linked list, insert a new node with value insertVal so the list stays sorted, and return a reference to the (possibly new) head. If the given reference is null, create a single self-pointing node and return it.
Open official problem prompt ↗Splice a new value into a circular sorted linked list so the non-decreasing order is preserved, no matter which node you were handed.
Adding a new hour label to a clock face that runs 1..12 and wraps back to 1; you place it in the correct arc, and if it is larger than 12 or smaller than 1 it goes at the 12-to-1 seam.
- Input
- head = [3,4,1] (a circular list 3 -> 4 -> 1 -> back to 3), insertVal = 2
- Output
- [3,4,1,2]
- Why
- In sorted order the values are 1,3,4; inserting 2 between 1 and 3 keeps it sorted, giving the cycle 3 -> 4 -> 1 -> 2 -> back to 3.
The number of nodes is in the range [0, 5 * 10^4]-10^6 <= Node.val <= 10^6-10^6 <= insertVal <= 10^6The list is sorted in non-decreasing order and is circular