Skip to content

Commit 221bdec

Browse files
authored
feat: add solutions to lc/lcof2 problems (#639)
lcof2 No.029 & lc No.0708.Insert into a Sorted Circular Linked List
1 parent 1010bad commit 221bdec

File tree

5 files changed

+325
-0
lines changed

5 files changed

+325
-0
lines changed

lcof2/剑指 Offer II 029. 排序的循环链表/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,73 @@ class Solution {
148148
}
149149
```
150150

151+
### **C++**
152+
153+
```cpp
154+
/*
155+
// Definition for a Node.
156+
class Node {
157+
public:
158+
int val;
159+
Node* next;
160+
161+
Node() {}
162+
163+
Node(int _val) {
164+
val = _val;
165+
next = NULL;
166+
}
167+
168+
Node(int _val, Node* _next) {
169+
val = _val;
170+
next = _next;
171+
}
172+
};
173+
*/
174+
175+
class Solution {
176+
public:
177+
Node* insert(Node* head, int insertVal) {
178+
Node* insert = new Node(insertVal);
179+
if (head == nullptr) {
180+
head = insert;
181+
head->next = head;
182+
} else if (head->next == nullptr) {
183+
head->next = insert;
184+
insert->next = head;
185+
} else {
186+
insertCore(head, insert);
187+
}
188+
189+
return head;
190+
}
191+
192+
void insertCore(Node* head, Node* insert) {
193+
Node* cur = head;
194+
Node* maxNode = head;
195+
Node* next = head->next;
196+
197+
while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
198+
cur = cur->next;
199+
next = next->next;
200+
201+
if (cur->val >= maxNode->val)
202+
maxNode = cur;
203+
}
204+
205+
if (cur->val <= insert->val && insert->val <= next->val) {
206+
insert->next = next;
207+
cur->next = insert;
208+
} else {
209+
insert->next = maxNode->next;
210+
maxNode->next = insert;
211+
212+
}
213+
214+
}
215+
};
216+
```
217+
151218
### **...**
152219
153220
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* next;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
next = NULL;
13+
}
14+
15+
Node(int _val, Node* _next) {
16+
val = _val;
17+
next = _next;
18+
}
19+
};
20+
*/
21+
22+
class Solution {
23+
public:
24+
Node* insert(Node* head, int insertVal) {
25+
Node* insert = new Node(insertVal);
26+
if (head == nullptr) {
27+
head = insert;
28+
head->next = head;
29+
} else if (head->next == nullptr) {
30+
head->next = insert;
31+
insert->next = head;
32+
} else {
33+
insertCore(head, insert);
34+
}
35+
36+
return head;
37+
}
38+
39+
void insertCore(Node* head, Node* insert) {
40+
Node* cur = head;
41+
Node* maxNode = head;
42+
Node* next = head->next;
43+
44+
while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
45+
cur = cur->next;
46+
next = next->next;
47+
48+
if (cur->val >= maxNode->val)
49+
maxNode = cur;
50+
}
51+
52+
if (cur->val <= insert->val && insert->val <= next->val) {
53+
insert->next = next;
54+
cur->next = insert;
55+
} else {
56+
insert->next = maxNode->next;
57+
maxNode->next = insert;
58+
59+
}
60+
61+
}
62+
};

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

+67
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,73 @@ class Solution {
138138
}
139139
```
140140

141+
### **C++**
142+
143+
```cpp
144+
/*
145+
// Definition for a Node.
146+
class Node {
147+
public:
148+
int val;
149+
Node* next;
150+
151+
Node() {}
152+
153+
Node(int _val) {
154+
val = _val;
155+
next = NULL;
156+
}
157+
158+
Node(int _val, Node* _next) {
159+
val = _val;
160+
next = _next;
161+
}
162+
};
163+
*/
164+
165+
class Solution {
166+
public:
167+
Node* insert(Node* head, int insertVal) {
168+
Node* insert = new Node(insertVal);
169+
if (head == nullptr) {
170+
head = insert;
171+
head->next = head;
172+
} else if (head->next == nullptr) {
173+
head->next = insert;
174+
insert->next = head;
175+
} else {
176+
insertCore(head, insert);
177+
}
178+
179+
return head;
180+
}
181+
182+
void insertCore(Node* head, Node* insert) {
183+
Node* cur = head;
184+
Node* maxNode = head;
185+
Node* next = head->next;
186+
187+
while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
188+
cur = cur->next;
189+
next = next->next;
190+
191+
if (cur->val >= maxNode->val)
192+
maxNode = cur;
193+
}
194+
195+
if (cur->val <= insert->val && insert->val <= next->val) {
196+
insert->next = next;
197+
cur->next = insert;
198+
} else {
199+
insert->next = maxNode->next;
200+
maxNode->next = insert;
201+
202+
}
203+
204+
}
205+
};
206+
```
207+
141208
### **...**
142209
143210
```

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

+67
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,73 @@ class Solution {
127127
}
128128
```
129129

130+
### **C++**
131+
132+
```cpp
133+
/*
134+
// Definition for a Node.
135+
class Node {
136+
public:
137+
int val;
138+
Node* next;
139+
140+
Node() {}
141+
142+
Node(int _val) {
143+
val = _val;
144+
next = NULL;
145+
}
146+
147+
Node(int _val, Node* _next) {
148+
val = _val;
149+
next = _next;
150+
}
151+
};
152+
*/
153+
154+
class Solution {
155+
public:
156+
Node* insert(Node* head, int insertVal) {
157+
Node* insert = new Node(insertVal);
158+
if (head == nullptr) {
159+
head = insert;
160+
head->next = head;
161+
} else if (head->next == nullptr) {
162+
head->next = insert;
163+
insert->next = head;
164+
} else {
165+
insertCore(head, insert);
166+
}
167+
168+
return head;
169+
}
170+
171+
void insertCore(Node* head, Node* insert) {
172+
Node* cur = head;
173+
Node* maxNode = head;
174+
Node* next = head->next;
175+
176+
while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
177+
cur = cur->next;
178+
next = next->next;
179+
180+
if (cur->val >= maxNode->val)
181+
maxNode = cur;
182+
}
183+
184+
if (cur->val <= insert->val && insert->val <= next->val) {
185+
insert->next = next;
186+
cur->next = insert;
187+
} else {
188+
insert->next = maxNode->next;
189+
maxNode->next = insert;
190+
191+
}
192+
193+
}
194+
};
195+
```
196+
130197
### **...**
131198
132199
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* next;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
next = NULL;
13+
}
14+
15+
Node(int _val, Node* _next) {
16+
val = _val;
17+
next = _next;
18+
}
19+
};
20+
*/
21+
22+
class Solution {
23+
public:
24+
Node* insert(Node* head, int insertVal) {
25+
Node* insert = new Node(insertVal);
26+
if (head == nullptr) {
27+
head = insert;
28+
head->next = head;
29+
} else if (head->next == nullptr) {
30+
head->next = insert;
31+
insert->next = head;
32+
} else {
33+
insertCore(head, insert);
34+
}
35+
36+
return head;
37+
}
38+
39+
void insertCore(Node* head, Node* insert) {
40+
Node* cur = head;
41+
Node* maxNode = head;
42+
Node* next = head->next;
43+
44+
while (!(cur->val <= insert->val && insert->val <= next->val) && next != head) {
45+
cur = cur->next;
46+
next = next->next;
47+
48+
if (cur->val >= maxNode->val)
49+
maxNode = cur;
50+
}
51+
52+
if (cur->val <= insert->val && insert->val <= next->val) {
53+
insert->next = next;
54+
cur->next = insert;
55+
} else {
56+
insert->next = maxNode->next;
57+
maxNode->next = insert;
58+
59+
}
60+
61+
}
62+
};

0 commit comments

Comments
 (0)