1.题目
给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
2.代码
public boolean isPail (ListNode head) {
// write code here
if (head!=null&&head.next==null){
return true;
}
List<Integer> lists = new ArrayList<>();
Stack<Integer> list = new Stack<>();
while (head!=null){
list.push(head.val);
lists.add(head.val);
head = head.next;
}
int i=0;
while(!list.empty()){
if (!list.pop().equals(lists.get(i))){
return false;
}
i++;
}
return true;
}