Skip to content

Commit b858f3a

Browse files
committed
Sync LeetCode submission Runtime - 38 ms (73.39%), Memory - 18.6 MB (7.64%)
1 parent c7b6ac7 commit b858f3a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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>&nbsp;</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+
&nbsp;
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&nbsp;<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>&nbsp;</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> &lt;= Node.val, insertVal &lt;= 10<sup>6</sup></code></li>
41+
</ul>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
"""
5+
# Definition for a Node.
6+
class Node:
7+
def __init__(self, val=None, next=None):
8+
self.val = val
9+
self.next = next
10+
"""
11+
12+
class Solution:
13+
def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
14+
15+
# If the list is empty
16+
if not head:
17+
new_node = Node(insertVal)
18+
new_node.next = new_node
19+
return new_node
20+
21+
curr = head
22+
23+
while True:
24+
# Find a position between two nodes where insertVal fits
25+
if curr.val <= insertVal <= curr.next.val:
26+
break
27+
28+
# When we reach the maximum value node (before minimum)
29+
elif curr.val > curr.next.val:
30+
if insertVal >= curr.val or insertVal <= curr.next.val:
31+
break
32+
33+
curr = curr.next
34+
35+
# We've completed one full cycle
36+
if curr == head:
37+
break
38+
39+
# Insert new node
40+
new_node = Node(insertVal)
41+
new_node.next = curr.next
42+
curr.next = new_node
43+
44+
return head
45+

0 commit comments

Comments
 (0)