import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { // write code here // 解题思路:如果是回文结构的话,那么必定会出现第i个节点的值和倒数第i个节点的值相同 // 所以遍历链表把值放进一个list 集合 // 判定 list.get(i) == list.get(size - i -1); // 如果链表只有一个节点也判定为回文结构 if (head == null || head.next == null) { return true; } List<Integer> valList = new ArrayList<>(); while (head != null) { valList.add(head.val); head = head.next; } for(int i =0;i<valList.size()/2;i++){ if(valList.get(i).intValue() != valList.get(valList.size()-i-1).intValue()){ return false; } } return true; } }