题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stack>
class Solution {
public:
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
if (head->next==nullptr) {
return true;
}
ListNode* point=head;
stack<int> st;
while (point!=nullptr) {
st.push(point->val);
point=point->next;
}
while (head!=nullptr) {
int temp=st.top();
st.pop();
if (temp!=head->val) {
return false;
}
head=head->next;
}
return true;
}
};
查看2道真题和解析
