|
6 | 6 |
|
7 | 7 | <p>Given a node from a <strong>Circular Linked List</strong> which is sorted in ascending 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 <em>any</em> single node in the list, and may not be necessarily the smallest value in the circular list.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <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>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <p>If the list is empty (i.e., 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 original given node.</p>
|
16 | 12 |
|
17 |
| - |
18 | 13 | <p> </p>
|
19 | 14 | <p><strong>Example 1:</strong></p>
|
20 | 15 | <img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0708.Insert%20into%20a%20Sorted%20Circular%20Linked%20List/images/example_1_before_65p.jpg" style="width: 250px; height: 149px;" /><br />
|
|
52 | 47 | <li><code>-10^6 <= insertVal <= 10^6</code></li>
|
53 | 48 | </ul>
|
54 | 49 |
|
55 |
| - |
56 | 50 | ## Solutions
|
57 | 51 |
|
58 | 52 | <!-- tabs:start -->
|
59 | 53 |
|
60 | 54 | ### **Python3**
|
61 | 55 |
|
62 | 56 | ```python
|
63 |
| - |
| 57 | +""" |
| 58 | +# Definition for a Node. |
| 59 | +class Node: |
| 60 | + def __init__(self, val=None, next=None): |
| 61 | + self.val = val |
| 62 | + self.next = next |
| 63 | +""" |
| 64 | + |
| 65 | +class Solution: |
| 66 | + def insert(self, head: 'Node', insertVal: int) -> 'Node': |
| 67 | + node = Node(val=insertVal) |
| 68 | + if head is None: |
| 69 | + node.next = node |
| 70 | + return node |
| 71 | + pre, cur = head, head.next |
| 72 | + while 1: |
| 73 | + if pre.val <= insertVal <= cur.val or (pre.val > cur.val and (insertVal >= pre.val or insertVal <= cur.val)): |
| 74 | + break |
| 75 | + pre, cur = cur, cur.next |
| 76 | + if pre == head: |
| 77 | + break |
| 78 | + pre.next = node |
| 79 | + node.next = cur |
| 80 | + return head |
64 | 81 | ```
|
65 | 82 |
|
66 | 83 | ### **Java**
|
67 | 84 |
|
68 | 85 | ```java
|
69 |
| - |
| 86 | +/* |
| 87 | +// Definition for a Node. |
| 88 | +class Node { |
| 89 | + public int val; |
| 90 | + public Node next; |
| 91 | +
|
| 92 | + public Node() {} |
| 93 | +
|
| 94 | + public Node(int _val) { |
| 95 | + val = _val; |
| 96 | + } |
| 97 | +
|
| 98 | + public Node(int _val, Node _next) { |
| 99 | + val = _val; |
| 100 | + next = _next; |
| 101 | + } |
| 102 | +}; |
| 103 | +*/ |
| 104 | + |
| 105 | +class Solution { |
| 106 | + public Node insert(Node head, int insertVal) { |
| 107 | + Node node = new Node(insertVal); |
| 108 | + if (head == null) { |
| 109 | + node.next = node; |
| 110 | + return node; |
| 111 | + } |
| 112 | + Node pre = head, cur = head.next; |
| 113 | + while (true) { |
| 114 | + if ((pre.val <= insertVal && insertVal <= cur.val) || (pre.val > cur.val && (insertVal >= pre.val || cur.val >= insertVal))) { |
| 115 | + break; |
| 116 | + } |
| 117 | + pre = cur; |
| 118 | + cur = cur.next; |
| 119 | + if (pre == head) { |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + pre.next = node; |
| 124 | + node.next = cur; |
| 125 | + return head; |
| 126 | + } |
| 127 | +} |
70 | 128 | ```
|
71 | 129 |
|
72 | 130 | ### **...**
|
|
0 commit comments