题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
import java.util.*; /* 投机取巧 * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { List<Integer> list = new ArrayList(); List<Integer> listReverse= new ArrayList(); ListNode p = head; if(p.next==null) return true; while(p!=null){ list.add(p.val); p = p.next; } listReverse.addAll(list); Collections.reverse(listReverse); String str = list.toString(); String strReverse = listReverse.toString(); if(str.equals(strReverse)){ return true; } return false; } }