这题的主要思路
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public boolean isPalindrome(ListNode head) { ListNode fast = head; ListNode slow = head; Stackstack = new Stack (); /** * 将链表的前半部分元素装入栈中,当快速runner *(移动的速度是慢速runner的两倍) * 到底链表尾部时,则慢速runner已经处于链表中间位置 */ while(fast != null && fast.next != null){ stack.push(slow.val); slow = slow.next; fast = fast.next.next; } //当链表为奇数个时,跳过中间元素 if (fast != null) { slow = slow.next; } while(slow != null){ //如果两者不相同,则该链表不是回文串 if (stack.pop() != slow.val) { return false; } slow = slow.next; } return true; }}