当前位置:网站首页>BM13判断一个链表是否为回文结构
BM13判断一个链表是否为回文结构
2022-08-10 21:49:00 【干饭小白】
这种水题有很多种解法:
我直接用栈写的,思路就不过多解释了
一共就几行代码,闭着眼睛都能看明白
可优化内存:只将一般压入栈,反正我感觉没必要这么做,如果题目有内存限定可以优化下
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
//直接用栈写 okk
stack<int>sta;
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;
}
};
边栏推荐
- Shell programming specification and variables
- 力扣221题,最大正方形
- 阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL
- 国内Gravatar头像的完美替代方案Cravatar
- 特别的三杯鸡
- Redis 性能影响 - 异步机制和响应延迟
- Regular expression of shell programming and text processor
- Likou 221 questions, the largest square
- shell脚本循环语句for、while语句
- 3598. 二叉树遍历(华中科技大学考研机试题)
猜你喜欢
随机推荐
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
xshell (sed command)
国内Gravatar头像的完美替代方案Cravatar
Common interview questions for APP UI automation testing, maybe useful~
过滤器
xshell (sed 命令)
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
学会开会|成为有连接感组织的重要技能
边缘与云计算:哪种解决方案更适合您的连接设备?
一篇文章教你Pytest快速入门和基础讲解,一定要看
使用 Cloudreve 搭建私有云盘
Likou 221 questions, the largest square
JVM经典五十问,这下面试稳了
翻译科技论文,俄译中怎样效果好
win系统下pytorch深度学习环境安装
Redis Performance Impact - Asynchronous Mechanisms and Response Latency
labelme - block drag and drop events
直播课堂系统08-腾讯云对象存储和课程分类管理
MySQL Advanced Commands
谁是边缘计算服务的采购者?是这六个关键角色