题目:给
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
if(head == null || head.next == null) return true;
Stack<ListNode> stack = new Stack();
ListNode node = head;
while(node != null){
stack.push(node);
node = node.next;
}
while(!stack.isEmpty() && head != null){
ListNode tmp = stack.pop();
if(tmp.val != head.val) return false;
head = head.next;
}
return true;
}
}
定一个链表,请判断该链表是否为回文结构。回文是指该字符串正序逆序完全一致。