请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
题目标签:Linked List / Two Pointers
题目链接:LeetCode / LeetCode中国
Language | Runtime | Memory |
---|---|---|
cpp | 8 ms | 4.5 MB |
/**
* 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 == NULL || head->next == NULL) {
return true;
}
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* p = dummy;
ListNode* q = head;
while (q && q->next) {
p = p->next;
q = q->next->next;
}
ListNode* r = p->next;
ListNode* s = r->next;
if (s == NULL) {
return p->val == r->val;
}
ListNode* t = s->next;
p->next = NULL;
r->next = NULL;
while (true) {
s->next = r;
r = s;
s = t;
if (t == NULL) {
break;
} else {
t = t->next;
}
}
if (q == NULL) {
q = r;
}
p = head;
while (p != NULL && q != NULL) {
if (p->val != q->val) {
return false;
}
p = p->next;
q = q->next;
}
return true;
}
};
static auto _ = [](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();