判断一个链表是否为回文结构
题目描述
给定一个链表,请判断该链表是否为回文结构。
代码
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) {
// write code here
Deque<Integer> dp = new LinkedList();
while(head!=null){
dp.push(head.val);
head = head.next;
}
while(dp.size()>1){
//dp.pollFirst() != dp.pollLast()只能有一部分的通过率
if(!dp.pollFirst().equals(dp.pollLast())) return false;
}
return true;
}
}