当前位置:网站首页>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;
}
}边栏推荐
- Eureka self protection
- 【综合题】【数据库原理】
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- The difference between lambda and anonymous inner class
- Kotlin notes - popular knowledge points asterisk (*)
- Xctf mobile--app1 problem solving
- 剑指Offer06. 从尾到头打印链表
- Attack and defense world mobile--ph0en1x-100
- context. Getexternalfilesdir() is compared with the returned path
- ImportError: No module named examples. tutorials. mnist
猜你喜欢

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

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

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

Attack and defense world mobile--ph0en1x-100

(最新版) Wifi分销多开版+安装框架

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?

The latest version of blind box mall thinkphp+uniapp

剑指Offer05. 替换空格

剑指Offer06. 从尾到头打印链表

2021 autumn Information Security Experiment 1 (password and hiding technology)
随机推荐
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
Is it OK to open an account for online stock speculation? Is the fund safe?
RedHat5 安装Socket5代理服务器
01_ Using the concurrent tool class library, is thread safety safe
Airflow installation jump pit
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Cloud Computing future - native Cloud
最新版盲盒商城thinkphp+uniapp
Eureka self protection
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
写一个简单的nodejs脚本
[exercise 6] [Database Principle]
2020-11_ Technical experience set
The latest version of lottery blind box operation version
[download attached] password acquisition tool lazagne installation and use
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
2021 autumn Information Security Experiment 1 (password and hiding technology)
电压环对 PFC 系统性能影响分析
Sqoop1.4.4原生增量导入特性探秘