class Solution {
public boolean isPalindrome(ListNode head) {
Stack<ListNode> stack=new Stack<ListNode>();
ListNode cur=head;
while(cur!=null){
stack.push(cur);
cur=cur.next;
}
while(head!=null){
if(head.val!=stack.pop().val)
return false;
head=head.next;
}
return true;
}
}
用栈先把链表节点放进去,再从栈中取出节点和链表节点逐个比较。
本文介绍了一种使用栈来辅助判断链表是否为回文结构的方法。通过将链表节点依次压入栈中,再逐一弹出并与原链表节点进行对比,以此验证链表是否满足回文特性。
810

被折叠的 条评论
为什么被折叠?



