Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dfee25a

Browse files
committed
feat: add solutions to lcof2 problem: No.027.Palindrome Linked List
1 parent c7e583d commit dfee25a

File tree

16 files changed

+920
-30
lines changed

16 files changed

+920
-30
lines changed

‎lcof2/剑指 Offer II 026. 重排链表/README.md‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
相当于这 3 道问题,只需要 5 行代码将它们组合:
5353

54-
- [链表的中间结点](../../solution/0800-0899/0876.Middle%20of%20the%20Linked%20List/README.md)
55-
- [反转链表](../../solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
54+
- [链表的中间结点](/solution/0800-0899/0876.Middle%20of%20the%20Linked%20List/README.md)
55+
- [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
5656
- 合并两个链表
5757

5858
<!-- tabs:start -->
@@ -166,6 +166,71 @@ class Solution {
166166
}
167167
```
168168

169+
### **C++**
170+
171+
```cpp
172+
/**
173+
* Definition for singly-linked list.
174+
* struct ListNode {
175+
* int val;
176+
* ListNode *next;
177+
* ListNode() : val(0), next(nullptr) {}
178+
* ListNode(int x) : val(x), next(nullptr) {}
179+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
180+
* };
181+
*/
182+
class Solution {
183+
public:
184+
void reorderList(ListNode* head) {
185+
ListNode* mid = middleNode(head);
186+
ListNode* tmp = mid->next;
187+
mid->next = nullptr;
188+
tmp = reverseList(tmp);
189+
head = mergeTwoLists(head, tmp);
190+
}
191+
192+
ListNode* middleNode(ListNode* head) {
193+
ListNode* slow = head;
194+
ListNode* fast = head;
195+
while (fast && fast->next)
196+
{
197+
slow = slow->next;
198+
fast = fast->next->next;
199+
}
200+
return slow;
201+
}
202+
203+
ListNode* reverseList(ListNode* head) {
204+
ListNode* pre = nullptr;
205+
ListNode* cur = head;
206+
while (cur)
207+
{
208+
ListNode* tmp = cur->next;
209+
cur->next = pre;
210+
pre = cur;
211+
cur = tmp;
212+
}
213+
return pre;
214+
}
215+
216+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
217+
ListNode* dummy = new ListNode();
218+
ListNode* cur = dummy;
219+
while (l1 && l2)
220+
{
221+
cur->next = l1;
222+
l1 = l1->next;
223+
cur = cur->next;
224+
cur->next = l2;
225+
l2 = l2->next;
226+
cur = cur->next;
227+
}
228+
cur->next = l1 ? l1 : l2;
229+
return dummy->next;
230+
}
231+
};
232+
```
233+
169234
### **Go**
170235
171236
```go
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
void reorderList(ListNode* head) {
14+
ListNode* mid = middleNode(head);
15+
ListNode* tmp = mid->next;
16+
mid->next = nullptr;
17+
tmp = reverseList(tmp);
18+
head = mergeTwoLists(head, tmp);
19+
}
20+
21+
ListNode* middleNode(ListNode* head) {
22+
ListNode* slow = head;
23+
ListNode* fast = head;
24+
while (fast && fast->next)
25+
{
26+
slow = slow->next;
27+
fast = fast->next->next;
28+
}
29+
return slow;
30+
}
31+
32+
ListNode* reverseList(ListNode* head) {
33+
ListNode* pre = nullptr;
34+
ListNode* cur = head;
35+
while (cur)
36+
{
37+
ListNode* tmp = cur->next;
38+
cur->next = pre;
39+
pre = cur;
40+
cur = tmp;
41+
}
42+
return pre;
43+
}
44+
45+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
46+
ListNode* dummy = new ListNode();
47+
ListNode* cur = dummy;
48+
while (l1 && l2)
49+
{
50+
cur->next = l1;
51+
l1 = l1->next;
52+
cur = cur->next;
53+
cur->next = l2;
54+
l2 = l2->next;
55+
cur = cur->next;
56+
}
57+
cur->next = l1 ? l1 : l2;
58+
return dummy->next;
59+
}
60+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /