-
Notifications
You must be signed in to change notification settings - Fork 0
/
234-回文链表.cpp
53 lines (52 loc) · 1.33 KB
/
234-回文链表.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
/*
* @lc app=leetcode.cn id=234 lang=cpp
*
* [234] 回文链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* 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)
{
if (!head)
return true;
auto slow = head;
auto fast = head;
auto slow_next = slow->next;
decltype(slow) slow_pre = nullptr;
bool isOdd = false;
while (fast) {
fast = fast->next;
if (!fast) {
isOdd = true;
break;
}
fast = fast->next;
slow->next = slow_pre;
slow_pre = slow;
slow = slow_next;
slow_next = slow->next;
}
decltype(slow) mid0 = slow_pre;
decltype(slow) mid1 = isOdd ? slow_next : slow;
while (mid0 && mid1) {
if (mid0->val != mid1->val)
return false;
mid0 = mid0->next;
mid1 = mid1->next;
}
return mid0 || mid1 ? false : true;
}
};
// @lc code=end