当前位置:网站首页>Leetcode234 palindrome linked list
Leetcode234 palindrome linked list
2022-07-03 12:49:00 【Mcc_ mingchao】

First check the title , The so-called palindrome , It's the same for forward traversal and backward traversal , It's easy to think , If I find the intermediate node , The number of each node from the middle to the back and from the middle to the front should be the same . This problem can be said to be an upgraded version of the reverse linked list . We need to use the idea of a speed pointer , If I move the pointer forward two digits at a time , The slow pointer advances one bit at a time , Fast pointer to the end , The slow pointer must be in the middle .
Pictured The length is odd and even

class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
ListNode slow = head, fast = head;
ListNode pre = head, prepre = null;
while(fast != null && fast.next != null) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if(fast != null) {
slow = slow.next;
}
while(pre != null && slow != null) {
if(pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}边栏推荐
- 基于Linu开发的项目视频
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
- Sword finger offer07 Rebuild binary tree
- 【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
- 基于同步坐标变换的谐波电流检测
- Detailed explanation of the most complete constraintlayout in history
- [exercise 7] [Database Principle]
- Record your vulnhub breakthrough record
- It feels great to know you learned something, isn‘t it?
猜你喜欢

社交社区论坛APP超高颜值UI界面

Eureka self protection

Record your vulnhub breakthrough record

Differences between initial, inherit, unset, revert and all

记录自己vulnhub闯关记录

Idea packages the web project into a war package and deploys it to the server to run

剑指Offer09. 用两个栈实现队列

Low code platform international multilingual (I18N) technical solution

自抗扰控制器七-二阶 LADRC-PLL 结构设计
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]
随机推荐
It feels great to know you learned something, isn‘t it?
idea将web项目打包成war包并部署到服务器上运行
Tensorflow binary installation & Failure
Apache Mina开发手册
Express abstract classes and methods
Applet wxss introduction
Xctf mobile--app1 problem solving
The latest version of blind box mall thinkphp+uniapp
How to convert a decimal number to binary in swift
Public and private account sending prompt information (user microservice -- message microservice)
Kotlin notes - popular knowledge points asterisk (*)
LeetCode 0556. Next bigger element III - end of step 4
Exploration of sqoop1.4.4 native incremental import feature
Implement verification code verification
Project video based on Linu development
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
The difference between lambda and anonymous inner class
Apache Mina Development Manual
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
Quickly learn member inner classes and local inner classes