|
| 1 | +<p>Given a Circular Linked List node, which is sorted in non-descending order, write a function to insert a value <code>insertVal</code> into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list and may not necessarily be the smallest value in the circular list.</p> |
| 2 | + |
| 3 | +<p>If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.</p> |
| 4 | + |
| 5 | +<p>If the list is empty (i.e., the given node is <code>null</code>), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the originally given node.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/01/19/example_1_before_65p.jpg" style="width: 250px; height: 149px;" /><br /> |
| 10 | + |
| 11 | +<pre> |
| 12 | +<strong>Input:</strong> head = [3,4,1], insertVal = 2 |
| 13 | +<strong>Output:</strong> [3,4,1,2] |
| 14 | +<strong>Explanation:</strong> In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3. |
| 15 | + |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/01/19/example_1_after_65p.jpg" style="width: 250px; height: 149px;" /> |
| 17 | + |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> head = [], insertVal = 1 |
| 24 | +<strong>Output:</strong> [1] |
| 25 | +<strong>Explanation:</strong> The list is empty (given head is <code>null</code>). We create a new single circular list and return the reference to that single node. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 3:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> head = [1], insertVal = 0 |
| 32 | +<strong>Output:</strong> [1,0] |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong>Constraints:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> |
| 40 | + <li><code>-10<sup>6</sup> <= Node.val, insertVal <= 10<sup>6</sup></code></li> |
| 41 | +</ul> |
0 commit comments