-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode234.cpp
170 lines (159 loc) · 4.06 KB
/
leetcode234.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*************************************************
Author: wenhaofang
Date: 2023-03-23
Description: leetcode234 - Palindrome Linked List
*************************************************/
#include <bits/stdc++.h>
using namespace std;
/**
* 方法一:转数组
*
* 理论时间复杂度:O(n),其中 n 为链表长度
* 理论空间复杂度:O(n),其中 n 为链表长度
*/
// struct ListNode {
// int val;
// ListNode *next;
// ListNode() : val(0), next(nullptr) {}
// ListNode(int x) : val(x), next(nullptr) {}
// ListNode(int x, ListNode *next) : val(x), next(next) {}
// };
// class Solution {
// public:
// bool isPalindrome(ListNode* head) {
// vector<int> nums;
// while (head) {
// nums.push_back(head -> val);
// head = head -> next;
// }
// int n = nums.size();
// if (n % 2 == 0) {
// int i = n / 2 - 1;
// int j = n / 2;
// while (i >= 0 && j <= n - 1) {
// if (nums[i] != nums[j]) {
// return false;
// }
// i--;
// j++;
// }
// }
// else {
// int k = n / 2;
// int i = k - 1;
// int j = k + 1;
// while (i >= 0 && j <= n - 1) {
// if (nums[i] != nums[j]) {
// return false;
// }
// i--;
// j++;
// }
// }
// return true;
// }
// };
/**
* 方法二:反转链表
*
* 理论时间复杂度:O(n),其中 n 为链表长度
* 理论空间复杂度:O(1)
*/
/**
* 思路
*
* 反转一半链表之后比较,步骤如下:
*
* 1. 找出链表中点
* 2. 反转后半链表
* 3. 比较是否回文
* 4. 还原后半链表
* 5. 最后返回答案
*/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
bool isPalindrome(ListNode* head) {
// 1
ListNode* mid = findMid(head);
// 2
ListNode* hd2 = reverse(mid -> next);
// 3
ListNode* p1 = head;
ListNode* p2 = hd2;
bool ans = compare(p1, p2);
// 4
reverse(hd2);
// 5
return ans;
}
ListNode* findMid(ListNode* head) {
if (!head) {
return nullptr;
}
ListNode* slow = head;
ListNode* fast = head;
while (
fast -> next &&
fast -> next -> next
) {
slow = slow -> next;
fast = fast -> next -> next;
}
return slow;
}
// leetcode206
ListNode* reverse(ListNode* head) {
if (!(head)) {
return nullptr;
}
if (!(head -> next)) {
return head;
}
ListNode* n1 = head;
ListNode* n2 = head -> next;
ListNode* n3 = head -> next -> next;
head -> next = nullptr;
while (1) {
n2 -> next = n1;
n1 = n2;
n2 = n3;
if (n3 == nullptr) {
break;
}
n3 = n3 -> next;
}
return n1;
}
bool compare(ListNode* p1, ListNode* p2) {
while (p1 && p2) {
if (p1 -> val != p2 -> val) {
return false;
}
p1 = p1 -> next;
p2 = p2 -> next;
}
return true;
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
ListNode* n4 = new ListNode(1);
ListNode* n3 = new ListNode(2);
ListNode* n2 = new ListNode(2);
ListNode* n1 = new ListNode(1);
n3 -> next = n4;
n2 -> next = n3;
n1 -> next = n2;
bool ans = solution -> isPalindrome(n1);
cout << ans << endl;
}