当前位置:网站首页>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;
}
}边栏推荐
- 02_ Lock the code, and don't let the "lock" become a worry
- (latest version) WiFi distribution multi format + installation framework
- 2020-10_ Development experience set
- CNN MNIST handwriting recognition
- Powerful avatar making artifact wechat applet
- Sword finger offer03 Repeated numbers in the array [simple]
- Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
- 并网-低电压穿越与孤岛并存分析
- Lambda expression
- Write a simple nodejs script
猜你喜欢
随机推荐
studio All flavors must now belong to a named flavor dimension. Learn more
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
【ManageEngine】IP地址扫描的作用
OpenStack节点地址改变
alright alright alright
Ali & ant self developed IDE
Enable SASL authentication for memcached
Lambda expression
Kotlin notes - popular knowledge points asterisk (*)
Swift bit operation exercise
电压环对 PFC 系统性能影响分析
基于Linu开发的项目视频
十條職場規則
剑指Offer07. 重建二叉树
并网-低电压穿越与孤岛并存分析
【数据挖掘复习题】
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
Eureka self protection
Sword finger offer09 Implementing queues with two stacks
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素







