Skip to content

Commit 7c02c0a

Browse files
committed
feat: add solutions to lc problem: No.0708. Insert into a Sorted Circular Linked List
1 parent d57461f commit 7c02c0a

File tree

5 files changed

+208
-19
lines changed

5 files changed

+208
-19
lines changed

Diff for: solution/0700-0799/0708.Insert into a Sorted Circular Linked List/README.md

+66-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
<li><code>-10^6 <= insertVal <= 10^6</code></li>
5353
</ul>
5454

55-
5655
## 解法
5756

5857
<!-- 这里可写通用的实现逻辑 -->
@@ -64,15 +63,79 @@
6463
<!-- 这里可写当前语言的特殊实现逻辑 -->
6564

6665
```python
67-
66+
"""
67+
# Definition for a Node.
68+
class Node:
69+
def __init__(self, val=None, next=None):
70+
self.val = val
71+
self.next = next
72+
"""
73+
74+
class Solution:
75+
def insert(self, head: 'Node', insertVal: int) -> 'Node':
76+
node = Node(val=insertVal)
77+
if head is None:
78+
node.next = node
79+
return node
80+
pre, cur = head, head.next
81+
while 1:
82+
if pre.val <= insertVal <= cur.val or (pre.val > cur.val and (insertVal >= pre.val or insertVal <= cur.val)):
83+
break
84+
pre, cur = cur, cur.next
85+
if pre == head:
86+
break
87+
pre.next = node
88+
node.next = cur
89+
return head
6890
```
6991

7092
### **Java**
7193

7294
<!-- 这里可写当前语言的特殊实现逻辑 -->
7395

7496
```java
75-
97+
/*
98+
// Definition for a Node.
99+
class Node {
100+
public int val;
101+
public Node next;
102+
103+
public Node() {}
104+
105+
public Node(int _val) {
106+
val = _val;
107+
}
108+
109+
public Node(int _val, Node _next) {
110+
val = _val;
111+
next = _next;
112+
}
113+
};
114+
*/
115+
116+
class Solution {
117+
public Node insert(Node head, int insertVal) {
118+
Node node = new Node(insertVal);
119+
if (head == null) {
120+
node.next = node;
121+
return node;
122+
}
123+
Node pre = head, cur = head.next;
124+
while (true) {
125+
if ((pre.val <= insertVal && insertVal <= cur.val) || (pre.val > cur.val && (insertVal >= pre.val || cur.val >= insertVal))) {
126+
break;
127+
}
128+
pre = cur;
129+
cur = cur.next;
130+
if (pre == head) {
131+
break;
132+
}
133+
}
134+
pre.next = node;
135+
node.next = cur;
136+
return head;
137+
}
138+
}
76139
```
77140

78141
### **...**

Diff for: solution/0700-0799/0708.Insert into a Sorted Circular Linked List/README_EN.md

+66-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66

77
<p>Given a node from a <strong>Circular Linked List</strong> which is sorted in ascending order,&nbsp;write a function to insert a value&nbsp;<code>insertVal</code> into the list such that it remains a&nbsp;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&nbsp;list.</p>
88

9-
10-
119
<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>
1210

13-
14-
1511
<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>
1612

17-
1813
<p>&nbsp;</p>
1914
<p><strong>Example 1:</strong></p>
2015
<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,21 +47,84 @@
5247
<li><code>-10^6 &lt;=&nbsp;insertVal &lt;= 10^6</code></li>
5348
</ul>
5449

55-
5650
## Solutions
5751

5852
<!-- tabs:start -->
5953

6054
### **Python3**
6155

6256
```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
6481
```
6582

6683
### **Java**
6784

6885
```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+
}
70128
```
71129

72130
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node next;
6+
7+
public Node() {}
8+
9+
public Node(int _val) {
10+
val = _val;
11+
}
12+
13+
public Node(int _val, Node _next) {
14+
val = _val;
15+
next = _next;
16+
}
17+
};
18+
*/
19+
20+
class Solution {
21+
public Node insert(Node head, int insertVal) {
22+
Node node = new Node(insertVal);
23+
if (head == null) {
24+
node.next = node;
25+
return node;
26+
}
27+
Node pre = head, cur = head.next;
28+
while (true) {
29+
if ((pre.val <= insertVal && insertVal <= cur.val) || (pre.val > cur.val && (insertVal >= pre.val || cur.val >= insertVal))) {
30+
break;
31+
}
32+
pre = cur;
33+
cur = cur.next;
34+
if (pre == head) {
35+
break;
36+
}
37+
}
38+
pre.next = node;
39+
node.next = cur;
40+
return head;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, next=None):
5+
self.val = val
6+
self.next = next
7+
"""
8+
9+
class Solution:
10+
def insert(self, head: 'Node', insertVal: int) -> 'Node':
11+
node = Node(val=insertVal)
12+
if head is None:
13+
node.next = node
14+
return node
15+
pre, cur = head, head.next
16+
while 1:
17+
if pre.val <= insertVal <= cur.val or (pre.val > cur.val and (insertVal >= pre.val or insertVal <= cur.val)):
18+
break
19+
pre, cur = cur, cur.next
20+
if pre == head:
21+
break
22+
pre.next = node
23+
node.next = cur
24+
return head

Diff for: solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62-
先求链表长度 n,再遍历链表,累加求得结果。
62+
遍历链表。
63+
64+
当遍历到链表某个结点,先将已有结果 res 乘以 2(即左移 1 位:`<< 1`),再加上当前结点的值,得出已遍历过的结点的十进制值。最后返回 res 即可。
6365

6466
<!-- tabs:start -->
6567

@@ -122,13 +124,13 @@ class Solution {
122124
* @param {ListNode} head
123125
* @return {number}
124126
*/
125-
var getDecimalValue = function(head) {
126-
let res = 0;
127-
while (head != null) {
128-
res = (res << 1) + head.val;
129-
head = head.next;
130-
}
131-
return res;
127+
var getDecimalValue = function (head) {
128+
let res = 0;
129+
while (head != null) {
130+
res = (res << 1) + head.val;
131+
head = head.next;
132+
}
133+
return res;
132134
};
133135
```
134136

0 commit comments

Comments
 (0)