当前位置:网站首页>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;
}
}边栏推荐
- 低代码平台国际化多语言(i18n)技术方案
- context. Getexternalfilesdir() is compared with the returned path
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- LeetCode 0556. Next bigger element III - end of step 4
- 基于Linu开发的项目视频
- 【ManageEngine】IP地址扫描的作用
- alright alright alright
- Differences between initial, inherit, unset, revert and all
- 剑指Offer09. 用两个栈实现队列
- Airflow installation jump pit
猜你喜欢
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use

Node. Js: use of express + MySQL
![[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
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

Integer case study of packaging

How to get user location in wechat applet?

并网-低电压穿越与孤岛并存分析

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

Ali & ant self developed IDE

Sword finger offer05 Replace spaces
随机推荐
The upward and downward transformation of polymorphism
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
最新版盲盒商城thinkphp+uniapp
双链笔记·思源笔记综合评测:优点、缺点、评价
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
How to convert a decimal number to binary in swift
Ten workplace rules
[embedded] - Introduction to four memory areas
【ManageEngine】IP地址扫描的作用
The solution to change the USB flash disk into a space of only 2m
node的ORM使用-Sequelize
Nodejs+express+mysql realizes login function (including verification code)
How to get user location in wechat applet?
Cloud Computing future - native Cloud
Xctf mobile--rememberother problem solving
剑指Offer07. 重建二叉树
Express abstract classes and methods
Public and private account sending prompt information (user microservice -- message microservice)
T430 toss and install OS majave 10.14
【综合题】【数据库原理】