8000 added 125. Reorder List · codingMan1216/LeetCode@6827426 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6827426

Browse files
committed
added 125. Reorder List
1 parent 43818fd commit 6827426

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

125. Reorder List/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
举例:
2+
3+
1->2->3->4->5->6->7->8
4+
5+
要求重排为:
6+
7+
1->8->2->7->3->6->4->5
8+
9+
看似摸不着头绪,单链表的最大问题不就是没法走回头路吗?
10+
11+
但如果我们将链表切成两部分呢?
12+
13+
1->2->3->4
14+
5->6->7->8
15+
16+
然后回忆一下链表基本操作,咦,是否将第二个链表逆序一下即可?
17+
18+
1->2->3->4
19+
8->7->6->5
20+
21+
到了这一步,差不多真相大白了吧。两个链表合并,貌似没一点难度。
22+
23+
------
24+
25+
**具体技术**
26+
27+
逆序的技巧不需多说,切成两部分,关键是找中点,这个可以利用我们常用的技巧:快慢指针。
28+
29+
1->2->3->4->5->6->7->8->null
30+
^ ^ ^ ^ ^ ^ ^ ^ ^
31+
s f | | | | | | |
32+
s | f | | | | | |
33+
s | f | | | | |
34+
s | | f | | | |
35+
s | f | | |
36+
s | f | |
37+
s f |
38+
s f
39+
40+
可以看到,`s` 正好停在指向 `4` 的地方。
41+
42+
逆序之后,链表应呈现以下情形:
43+
44+
1->2->3->4->8->7->6->5->null
45+
^ ^
46+
head slow
47+
48+
再进一步,将该链表分为两个:
49+
50+
1->2->3->4->null
51+
8->7->6->5->null
52+
53+
然后用咱们现成的 `shuffleMerge` 即可。

125. Reorder List/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "solution.h"
2+
#include <iostream>
3+
#include <initializer_list>
4+
using namespace std;
5+
6+
ListNode *create_linkedlist(std::initializer_list<int> lst)
7+
{
8+
auto iter = lst.begin();
9+
ListNode *head = lst.size() ? new ListNode(*iter++) : nullptr;
10+
for (ListNode *cur=head; iter != lst.end(); cur=cur->next)
11+
cur->next = new ListNode(*iter++);
12+
return head;
13+
}
14+
15+
void clear(ListNode *head)
16+
{
17+
while (head) {
18+
ListNode *del = head;
19+
head = head->next;
20+
delete del;
21+
}
22+
}
23+
24+
void printList(ListNode *head) {
25+
for (ListNode *cur = head; cur; cur = cur->next)
26+
cout << cur->val << "->";
27+
cout << "\b\b " << endl;
28+
}
29+
30+
int main()
31+
{
32+
Solution s;
33+
ListNode *a = create_linkedlist({1,2,3,4,5,6,7,8,9});
34+
s.reorderList(a);
35+
printList(a);
36+
clear(a);
37+
}

125. Reorder List/solution.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <cstddef>
2+
3+
struct ListNode {
4+
int val;
5+
ListNode *next;
6+
ListNode(int x) : val(x), next(NULL) {}
7+
};
8+
9+
class Solution {
10+
ListNode* reverseList(ListNode *head) {
11+
ListNode *newHead = NULL;
12+
while (head) {
13+
ListNode *next = head->next;
14+
head->next = newHead;
15+
newHead = head;
16+
head = next;
17+
}
18+
return newHead;
19+
}
20+
21+
ListNode *shuffleMerge(ListNode *a, ListNode *b) {
22+
ListNode *ret = NULL, **lastRef = &ret;
23+
while (a && b) {
24+
moveNode(lastRef, &a);
25+
lastRef = &((*lastRef)->next);
26+
moveNode(lastRef, &b);
27+
lastRef = &((*lastRef)->next);
28+
}
29+
*lastRef = a ? a : b;
30+
return ret;
31+
}
32+
33+
void moveNode(ListNode **destRef, ListNode **sourceRef) {
34+
ListNode *newNode = *sourceRef;
35+
*sourceRef = newNode->next;
36+
newNode->next = *destRef;
37+
*destRef = newNode;
38+
}
39+
40+
public:
41+
void reorderList(ListNode *head) {
42+
if (head == NULL) return;
43+
ListNode *slow = head, *fast = head->next;
44+
while (fast) {
45+
fast = fast->next;
46+
if (fast) {
47+
slow = slow->next;
48+
fast = fast->next;
49+
}
50+
}
51+
ListNode *back = reverseList(slow->next);
52+
slow->next = NULL;
53+
head = shuffleMerge(head, back);
54+
}
55+
};

0 commit comments

Comments
 (0)
0