-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathreorder_list.cpp
137 lines (113 loc) · 3.32 KB
/
reorder_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @Author: Chacha
* @Date: 2019-01-18 16:38:09
* @Last Modified by: Chacha
* @Last Modified time: 2019-01-18 18:50:29
*/
#include<iostream>
#include<string>
using namespace std;
/**
* Definition for singly-linked list(单向链表)
* Source: https://zh.wikipedia.org/wiki/链表
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
/**
* Given a singly linked list L: L0→L1→…→Ln-1→Ln,
* reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
*
* Note: You must do this in-place without altering the nodes' values.
*
* Example:
* Given 1->2->3->4->null, reorder it to 1->4->2->3->null.
*
* Source: https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73015
*/
void reorderList1(ListNode* head) {
if (head == NULL || head->next == NULL || head->next->next == NULL) {
return;
}
ListNode* last = head;
int length = 0;
while(last != NULL) {
last = last->next;
++length;
}
last = head;
for(int i = 1; i < length - i; ++i) {
ListNode* beforeTail = last;
for(int j = i; j < length - i; ++j) {
beforeTail = beforeTail->next;
}
ListNode* temp = last->next;
last->next = beforeTail->next;
last->next->next = temp;
beforeTail->next = NULL;
last = temp;
}
}
void reorderList2(ListNode* head) {
if (head == NULL || head->next == NULL || head->next->next) return;
ListNode* middle = findMiddle(head);
ListNode* right = reverse(middle->next);
middle->next = NULL;
merge(head, right);
}
void merge(ListNode* left, ListNode* right) {
ListNode* dummy = new ListNode(0);
while(left != NULL && right != NULL){
dummy->next = left;
left = left->next;
dummy = dummy->next;
dummy->next = right;
right = right->next;
dummy = dummy->next;
}
dummy->next = left != NULL ? left : right;
}
ListNode* reverse(ListNode* head) {
ListNode* newHead = NULL;
while(head != NULL){
ListNode* temp = head->next;
head->next = newHead;
newHead = head;
head = temp;
}
return newHead;
}
ListNode* findMiddle(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode *slow = head, *fast = head->next;
while (slow != NULL && fast != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
/* Function to print nodes in a given linked list */
void printList(ListNode *node) {
while (node != NULL) {
printf("%d ", node->val);
node = node->next;
}
}
int main() {
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
head->next->next->next->next = new ListNode(5);
head->next->next->next->next->next = new ListNode(6);
Solution().reorderList2(head);
printList(head);
return 0;
}