题目
代码
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null) {
return true;
}
ListNode syncopationPoint = earchSyncopationPoint(head);
ListNode reverseListNode = reverseList(syncopationPoint.next);
ListNode p1 = head;
ListNode p2 = reverseListNode;
boolean result = true;
while(result && p2 != null) {
if (p1.val != p2.val) {
result = false;
}
p1 = p1.next;
p2 = p2.next;
}
syncopationPoint.next = reverseList(reverseListNode);
return result;
}
private ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
private ListNode earchSyncopationPoint(ListNode head) {
ListNode fast = head, slow = head;
while (fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}