/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
//使用堆栈进行存储
ListNode* cur = head;
stack<ListNode*> s;
while(cur != NULL)
{
s.push(cur);
cur = cur->next;
}
while(!s.empty() && head != NULL)
{
ListNode* temp = s.top();
s.pop();
if(temp->val == head->val)
{
head = head->next;
}else{
return false;
}
}
return true;
}
};
BM13 判断一个链表是否为回文结构 C++
最新推荐文章于 2025-06-14 15:42:09 发布