-
Notifications
You must be signed in to change notification settings - Fork 1
/
234 Palindrome Linked List .cpp
55 lines (55 loc) · 1.31 KB
/
234 Palindrome Linked 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (!head) {
return true;
}
auto getLen = [] (ListNode *x) {
int s = 0;
while (x) {
s++;
x = x->next;
}
return s;
};
int len = getLen(head);
auto get = [] (ListNode *x, int k) {
while (k--) {
x = x->next;
}
return x;
};
if (len == 1) {
return true;
}
ListNode *h1 = head, *h2 = get(head, (len + 1) / 2 - 1);
auto next = h2->next;
h2->next = nullptr;
h2 = next;
auto reversed = [] (ListNode *x) {
ListNode *newHead = nullptr;
while (x) {
auto next = x->next;
x->next = newHead;
newHead = x;
x = next;
}
return newHead;
};
h2 = reversed(h2);
for (; h1 && h2; h1 = h1->next, h2 = h2->next) {
if (h1->val != h2->val) {
return false;
}
}
return true;
}
};