当前位置:网站首页>BM13 determines whether a linked list is a palindrome

BM13 determines whether a linked list is a palindrome

2022-08-10 22:29:00 dry rice white

There are many solutions to this water problem:

I wrote it directly with the stack, so I won't explain the idea too much

There are only a few lines of code in total, and you can understand it with your eyes closed

Optimizable memory: only push the general stack, anyway, I don't think it is necessary to do so, if the topic has memory constraints, it can be optimized

/*** struct ListNode {* int val;* struct ListNode *next;* };*/class Solution {public:/**** @param head ListNode class the head* @return bool boolean*/bool isPail(ListNode* head) {// write code here//Write okk directly on the stackstacksta;ListNode *p = head;while(p != nullptr){sta.push(p->val);p = p->next;}p = head;while(p != nullptr){if(p->val != sta.top()){break;}sta.pop();p = p->next;}if(p == nullptr){return true;}return false;}};

原网站

版权声明
本文为[dry rice white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102148592585.html