-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path234.cpp
More file actions
31 lines (30 loc) · 817 Bytes
/
Copy path234.cpp
File metadata and controls
31 lines (30 loc) · 817 Bytes
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
class Solution {
public:
bool isPalindrome(ListNode *head) {
ListNode *first = head, *second = head;
while (first && first->next) {
first = first->next->next;
second = second->next;
}
if (first)
second = second->next;
second = ReverseList(second);
while (head && second) {
if (head->val != second->val)
return false;
head = head->next;
second = second->next;
}
return true;
}
ListNode *ReverseList(ListNode *head) {
ListNode *prev = nullptr, *curr = head, *next = head;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};