当前位置:网站首页>LeetCode-234-回文链表
LeetCode-234-回文链表
2022-06-29 15:26:00 【z754916067】
题目

思路
- 链表不是回文的,指其中的数字有可能会回文。最直接的方法是遍历加入数组然后判断数组回不回文即可,但是这样的话太暴力了应该不是他们想要的解法。
- 回文的开始有一个特征:下一个和自己相等。
- 不对,也有可能不相等,但是根据中间的节点对称…
- 突然想到是否能够用栈,将遍历的数字压入栈中,再从头开始遍历的同时弹出栈,判断数字相不相等就行了。不过总感觉和最暴力的解法差不多。
代码
public boolean isPalindrome(ListNode head) {
Stack<Integer> s = new Stack<>();
ListNode temp = head;
while(temp!=null){
s.push(temp.val);
temp=temp.next;
}
//开始对比 重新遍历
while(head.next!=null){
if(head.val!=s.pop()) return false;
head=head.next;
}
return true;
}
边栏推荐
- PostgreSQL source code learning (23) -- transaction log ④ - log assembly
- MySQL scheduled full database backup & rolling deletion of backup data before the specified date
- MCS: multivariate random variable - discrete random variable
- swift JSONSerialization
- 雷达发射机
- 雷达相关内容简介
- Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
- radar transmitter
- js获取上个月第一天以及本月的最后一天
- GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021
猜你喜欢

Northwestern Polytechnic University attacked by overseas e-mail

西北工业大学遭境外电邮攻击

Take another picture of cloud redis' improvement path

MySQL JSON array operation JSON_ array_ append、json_ array_ insert

File common tool class, stream related application (record)

Lumiprobe reactive dye carboxylic acid: sulfo cyanine7.5 carboxylic acid

What is the relationship between synchronized and multithreading
![Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem](/img/f0/5c2504d51532dcda0ac115f3703384.gif)
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem

Differential equations of satellite motion

Middle order and post order traversal to construct binary tree [recursive partition interval and backtracking splicing subtree + similarity and difference between middle post order and middle pre orde
随机推荐
Development and application of NFT chain Games: Six noteworthy NFT trends in 2022
CKS CKA ckad change terminal to remote desktop
SSL V**技术原理
LeetCode笔记:Weekly Contest 299
Review of digital image processing
这是少了什么依赖嘛?FlinkSql打包运行的时候报错,但是本地idea跑的时候是没问题的,求解,谢
message from server: “Host ‘xxxxxx‘ is blocked because of many connection errors; unblock with ‘m
Mysql database naming conventions PDF
NFT链游开发应用:2022年值得关注的6大NFT趋势
File常用工具类, 流相关运用 (记录)
雷达天线简介
PostgreSQL source code learning (24) -- transaction log ⑤ - log writing to wal buffer
MySQL scheduled full database backup & rolling deletion of backup data before the specified date
Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
Scroll,你玩明白了嘛?
Implementing redis distributed locks using custom annotations
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
List集合详细讲解
无意发现的【TiDB缓存表】,竟能解决读写热点问题
Training mode of deep learning network