708 Insert into a Cyclic Sorted List
1. Question
Given a node from a cyclic linked list which is sorted in ascending order, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be a reference to_any_single node in the list, and may not be necessarily the smallest value in the cyclic list.
If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the cyclic list should remain sorted.
If the list is empty (i.e., given node isnull
), you should create a new single cyclic list and return the reference to that single node. Otherwise, you should return the original given node.
2. Implementation
3. Time & Space Complexity
时间复杂度O(n),空间复杂度O(1)
Last updated
Was this helpful?