Medium · Linked Lists

Insert into a Sorted Circular Linked List

Given a node of a circular linked list whose values are sorted in non-descending order (the given node need not be the smallest; the input lists values starting from it) and a value insertVal, insert a new node holding insertVal so the circle stays sorted and return the given node; if the list is empty, return the new node linked to itself.

Examples

Example 1

[3,4,1] insert 2

Output: [3, 4, 1, 2]

Example 2

[1,3,5] insert 0

Output: [1, 3, 5, 0]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Linked Lists problems