快慢指针+反转链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public boolean isPalindrome(ListNode head) { if (head == null || head.next == null) return true; ListNode slow,fast; slow = fast = head; while (fast != null && fast.next!=null){ slow = slow.next; fast = fast.next.next; } if (fast!=null) slow = slow.next; ListNode left = head; ListNode right = reverse(slow); while (right != null){ if(left.val != right.val) return false; left = left.next; right = right.next; } return true; } public ListNode reverse(ListNode a){ ListNode pre,cur,nxt; pre = null; cur = nxt = a; while (cur != null){ nxt = cur.next; cur.next = pre; pre = cur; cur = nxt; } return pre; } }