forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalindromeLinkedList.cpp
57 lines (50 loc) · 1.36 KB
/
PalindromeLinkedList.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
// Source : https://leetcode.com/problems/palindrome-linked-list/
// Author : Hao Chen
// Date : 2015-07-17
/**********************************************************************************
*
* Given a singly linked list, determine if it is a palindrome.
*
* Follow up:
* Could you do it in O(n) time and O(1) space?
*
**********************************************************************************/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findMiddle(ListNode* head) {
ListNode *p1=head, *p2=head;
while(p2 && p2->next){
p1 = p1->next;
p2 = p2->next->next;
}
return p1;
}
ListNode* reverseLink(ListNode* head) {
ListNode* p=NULL;
while (head) {
ListNode* q = head->next;
head->next = p;
p = head;
head = q;
}
return p;
}
bool isPalindrome(ListNode* head) {
ListNode* pMid = findMiddle(head);
ListNode* pRev = reverseLink(pMid);
for(;head!=pMid; head=head->next, pRev=pRev->next) {
if (head->val != pRev->val) {
return false;
}
}
return true;
}
};