题目:
解答:
一边反转链表一边移动指针
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
ListNode fast=head;
ListNode slow=head;
ListNode prepre;
ListNode pre=head;
prepre=null;
while(fast != null && fast.next != null){
pre=slow;
slow=slow.next;
fast=fast.next.next;
pre.next=prepre;
prepre=pre;
}
if(fast != null) {
slow = slow.next;
}
while(pre != null && slow != null) {
if(pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}