Skip to content

Commit b54b5d1

Browse files
committed
update
1 parent a6445e8 commit b54b5d1

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

ReorderList/ReorderList.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
//============================================================================
1616

1717
#include <iostream>
18+
1819
using namespace std;
20+
21+
1922
/**
2023
* Definition for singly-linked list.
2124
*/
@@ -25,7 +28,6 @@ struct ListNode {
2528
ListNode(int x) : val(x), next(NULL) {}
2629
};
2730

28-
2931
class Solution {
3032
public:
3133
void reorderList(ListNode *head) {
@@ -41,28 +43,34 @@ class Solution {
4143
curNode = curNode->next;
4244
}
4345

44-
int i = 0, j = vs.size()-1;
46+
int i = 0, j = vs.size() - 1;
4547
while (i < j) {
4648
vs[i]->next = vs[j];
47-
vs[j]->next = vs[i+1];
49+
vs[j]->next = vs[i + 1];
4850
i++, j--;
4951
}
5052
vs[i]->next = NULL;
5153
}
5254

5355
void reorderList2(ListNode *head) {
54-
if (head == NULL) return;
55-
ListNode * slowNode = head, *fastNode = head;
56-
while (fastNode->next != NULL && fastNode->next->next != NULL) slowNode = slowNode->next, fastNode = fastNode->next->next;
57-
if (slowNode == fastNode) return;
58-
fastNode = reverse(slowNode->next);
56+
if (head == NULL || head->next == NULL) return;
57+
ListNode * frontHead, *backHead;
58+
split(head, frontHead, backHead);
59+
backHead = reverse(backHead);
60+
merge(frontHead, backHead);
61+
}
62+
63+
void split(ListNode * head, ListNode *& frontHead, ListNode *& backHead) {
64+
ListNode * fastNode = head, *slowNode = head;
65+
for (; fastNode->next != NULL && fastNode->next->next != NULL; fastNode = fastNode->next->next) slowNode = slowNode->next;
66+
backHead = slowNode->next;
5967
slowNode->next = NULL;
60-
head = merge(head, fastNode);
68+
frontHead = head;
6169
}
6270

63-
ListNode * reverse(ListNode * curNode) {
64-
ListNode * preNode = NULL;
65-
while (curNode != NULL) {
71+
ListNode * reverse(ListNode * head) {
72+
ListNode * preNode = NULL, *curNode = head;
73+
while (curNode) {
6674
ListNode * nextNode = curNode->next;
6775
curNode->next = preNode;
6876
preNode = curNode;
@@ -71,21 +79,26 @@ class Solution {
7179
return preNode;
7280
}
7381

74-
ListNode * merge(ListNode * frontNode, ListNode * backNode) {
75-
ListNode * head = new ListNode(-1), *curNode = head;
76-
while (frontNode != NULL || backNode != NULL) {
77-
if (frontNode != NULL) curNode->next = frontNode, frontNode = frontNode->next, curNode = curNode->next;
78-
if (backNode != NULL) curNode->next = backNode, backNode = backNode->next, curNode = curNode->next;
82+
void merge(ListNode * frontHead, ListNode * backHead) {
83+
ListNode * head = pushDummy(NULL), *curNode = head;
84+
while (frontHead || backHead) {
85+
if (frontHead) curNode->next = frontHead, frontHead = frontHead->next, curNode = curNode->next;
86+
if (backHead) curNode->next = backHead, backHead = backHead->next, curNode = curNode->next;
7987
}
88+
head = popDummy(head);
89+
}
8090

81-
return deleteNode(head);
91+
ListNode * pushDummy(ListNode * head) {
92+
ListNode * newNode = new ListNode(-1);
93+
newNode->next = head;
94+
return newNode;
8295
}
8396

84-
ListNode * deleteNode(ListNode * curNode) {
85-
ListNode * toDel = curNode;
86-
curNode = curNode->next;
87-
delete toDel;
88-
return curNode;
97+
ListNode * popDummy(ListNode * head) {
98+
ListNode * delNode = head;
99+
head = head->next;
100+
delete delNode;
101+
return head;
89102
}
90103
};
91104

0 commit comments

Comments
 (0)