题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if (head==null || head.next == null){
return true;
}
// 容器存放元素
List<Integer> arr = new ArrayList<>();
while(head!=null){
arr.add(head.val);
head = head.next;
}
// 定义左右索引
int l = 0;int r = arr.size()-1;
while (l<=r){
if (!arr.get(l).equals(arr.get(r))){
return false;
}
l++;
r--;
}
return true;
}
}
