Problem · Linked List
Build a Sorted Circular Singly Linked List
Learn this problemProblem statement
Start with an empty circular singly linked list. Insert the integers from values one at a time. Each insertion must keep the list in nondecreasing order; duplicates are allowed. The head always stores a minimum value, and the tail's next pointer always points to the head. A one-node list therefore points to itself.
After all insertions, return one finite traversal beginning at the head. Return an empty array when no values were inserted.
Function
buildSortedCircularList(values: int[]) → int[]Examples
Example 1
values = [4,1,3,1]return = [1,1,3,4]Each value is inserted into its sorted position. Traversing once from the minimum-valued head yields the four values shown.
Example 2
values = []return = []No nodes are created for an empty insertion sequence.
Constraints
0 <= values.length <= 5000.-10^9 <= values[i] <= 10^9.- Build and maintain the requested linked structure; do not sort and return the input array directly.