当前位置:网站首页>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;
}
}边栏推荐
- Swift bit operation exercise
- 剑指Offer03. 数组中重复的数字【简单】
- Kung Fu pays off, and learning is done
- ORM use of node -serialize
- The latest version of lottery blind box operation version
- The upward and downward transformation of polymorphism
- Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
- Kotlin notes - popular knowledge points asterisk (*)
- 剑指Offer09. 用两个栈实现队列
- Cloud Computing future - native Cloud
猜你喜欢

Node.js: express + MySQL的使用

Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)

基于同步坐标变换的谐波电流检测

Social community forum app ultra-high appearance UI interface

社交社区论坛APP超高颜值UI界面
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

studio All flavors must now belong to a named flavor dimension. Learn more

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

Alibaba is bigger than sending SMS (user microservice - message microservice)

【数据库原理复习题】
随机推荐
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
TOGAF认证自学宝典V2.0
最新版盲盒商城thinkphp+uniapp
Cloud Computing future - native Cloud
Swift bit operation exercise
Differences between initial, inherit, unset, revert and all
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Solve the problem of VI opening files with ^m at the end
Openstack node address change
公纵号发送提示信息(用户微服务--消息微服务)
Method overloading and rewriting
Swift5.7 扩展 some 到泛型参数
Applet wxss introduction
elastic_ L02_ install
Eureka自我保护
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Simple use and precautions of kotlin's array array and set list
[ManageEngine] the role of IP address scanning
Xctf mobile--app2 problem solving
Write a simple nodejs script