As reported: Google L3 phone screen (London, 2025): a music playlist app whose songs form a circular queue sorted by popularity score in non-decreasing order. Each song knows the next song, and the last one links back to the first. Given a reference to any song in the playlist and a new song's score, insert it so the playlist stays sorted and circular.
Songs are nodes of a circular singly linked list, sorted in non-decreasing order of val (the popularity score) when read from the smallest value. You are given a reference head to any node and a value insertVal.
Insert a new node with insertVal so the list stays a sorted circular list, and return head. If the list is empty (head is null), create a single node that points to itself and return it.
This is LeetCode 708 — Insert into a Sorted Circular Linked List.
Example
head -> 3 -> 4 -> 1 -> (back to 3), insertVal = 2
-> 3 -> 4 -> 1 -> 2 -> (back to 3) return the node 3
head = null, insertVal = 1 -> 1 -> (itself)
head -> 1 -> (itself), insertVal = 0 -> 1 -> 0 -> (back to 1)
Clarifications
- If several places keep the list sorted, any of them is accepted.
Constraints: 0 ≤ number of nodes ≤ 5·10^4; −10^6 ≤ val, insertVal ≤ 10^6.
Code runner format: LeetCode's format: two lines, the list as a JSON array read from head, and insertVal. Implement insert(head, insertVal); the harness checks the list you return.